Python command not working in command prompt - python

When I type python into the command line, the command prompt says python is not recognized as an internal or external command, operable program, or batch file. What should I do?
Note: I have Python 2.7 and Python 3.2 installed on my computer.

I have installed the latest Python for Win10 from Releases for Windows.
Just typing py in the Command Prompt Window starts Python.
Microsoft Windows [Version 10.0.15048]
(c) 2017 Microsoft Corporation. All rights reserved.
C:\Users\sg7>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>`enter code here`
Testing:
>>> print("hello!")
hello!
>>>
Please be aware that in my case Python was installed in C:\Users\sg7\AppData\Local\Programs\Python\Python36> directory
C:\Users\sg7\AppData\Local\Programs\Python\Python36>dir
Volume in drive C is Windows7_OS
Volume Serial Number is 1226-12D1
Directory of C:\Users\sg7\AppData\Local\Programs\Python\Python36
08/05/2018 07:38 AM <DIR> .
08/05/2018 07:38 AM <DIR> ..
12/18/2017 09:12 AM <DIR> DLLs
12/18/2017 09:12 AM <DIR> Doc
12/18/2017 09:12 AM <DIR> include
12/18/2017 09:12 AM <DIR> Lib
12/18/2017 09:12 AM <DIR> libs
10/03/2017 07:17 PM 30,334 LICENSE.txt
10/03/2017 07:17 PM 362,094 NEWS.txt
10/03/2017 07:15 PM 100,504 python.exe
10/03/2017 07:12 PM 58,520 python3.dll
10/03/2017 07:12 PM 3,610,776 python36.dll
10/03/2017 07:15 PM 98,968 pythonw.exe
08/05/2018 07:38 AM 196,096 Removescons.exe
08/05/2018 07:38 AM 26,563 scons-wininst.log
08/05/2018 07:38 AM <DIR> Scripts
12/18/2017 09:12 AM <DIR> tcl
12/18/2017 09:12 AM <DIR> Tools
06/09/2016 11:53 PM 87,888 vcruntime140.dll
9 File(s) 4,571,743 bytes
10 Dir(s) 20,228,898,816 bytes free
When I am at C:\Users\sg7> directory level python can be invoked by typing
AppData\Local\Programs\Python\Python36\python
C:\Users\samg>AppData\Local\Programs\Python\Python36\python
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Newer Python 3.7 will be installed at:
C:\Users\YourUserNameHere\AppData\Local\Programs\Python\Python37
If you wish you can add to your path environment variable:
%USERPROFILE%\AppData\Local\Programs\Python\Python36

It finally worked!!!
I needed to do things to get it to work
Add C:\Python27\ to the end of the PATH system variable
Add C:\Python27\ to the end of the PYTHONPATH system variable
I had to add these to both for it to work.
If I added any subdirectories, it did not work for some reason.
Thank you all for your responses.

The video was very useful.
Go to system properties -> Advance ( or type "system env" in
start menu.)
Click environment variables
Edit the 'PATH' variable
Add 2 new paths 'C:\Python27' and 'C:\Python27\scripts'
Run cmd again and type python.
it worked for me

Rather than the command "python", consider launching Python via the py launcher, as described in sg7's answer, which runs your latest version of Python (or lets you select a specific version). The py launcher is enabled via a check box during installation (default: "on").
Nevertheless, you can still put the "python" command in your PATH, either at "first installation" or by "modifying" an existing installation.
First Installation:
Checking the "[x] Add Python x.y to PATH" box on the very first dialog. Here's how it looks in version 3.8:
This has the effect of adding the following to the PATH variable:
C:\Users\...\AppData\Local\Programs\Python\Python38-32\Scripts\
C:\Users\...\AppData\Local\Programs\Python\Python38-32\
Modifying an Existing Installation:
Re-run your installer (e.g. in Downloads, python-3.8.4.exe) and Select "Modify".
Check all the optional features you want (likely no changes), then click [Next]. Check [x] "Add Python to environment variables", and [Install].

In Windows 7 python start command in command prompt is
c:\>python3
but in Windows 10 python start command in command prompt is
C:\>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\>py --version
Python 3.6.3
C:\>
But in Windows 10 python3 syntax not work also not given any error.
Django start command also uses py instead of python3.
d:\>py manage.py runserver

In case someone else has the same issue, if you have already added to PATH and it (C:\Users\pete.kirkham\AppData\Local\Programs\Python\Python39\python) works if you run with a full path, on Win 10 the command line command python is by default aliased to the Windows store.
Go to settings > App execution aliases and disable the alias for python that runs App installer instead.
More details can be found in this question CMD opens window store when I type python
(presumably if your organisation doesn't disable the Windows Store and you install via the Store it disables this when you install, but installing via download from python.org does not)

This working for me :
PS C:\Users\MyUsername> py -V
Python 3.9.0

They gave us a script to do this for us already
C:\Users\hUTBER\AppData\Local\Programs\Python\tools\scripts\win_add2path.py
You'll need to make sure that you close and open the cmd otherwise it won't have the new path there.
If you can't find this script these are the paths that it will add and I had to add manually in the end.
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35\Scripts
Where mine and now python works in the cmd

Python 3.10 uses py and not python.
Try py --version if you are using this version.

Even after following the instructions from the valuable answers above, calling python from the command line would open the Microsoft Store and redirect me to a page to download the software.
I discovered this was caused by a 0 Ko python.exe file in AppData\Local\Microsoft\WindowsApps which was taking precedence over my python executable in my PATH.
Removing this folder from my PATH solved it.

Kalle posted a link to a page that has this video on it, but it's done on XP. If you use Windows 7:
Press the windows key.
Type "system env". Press enter.
Press alt + n
Press alt + e
Press right, and then ; (that's a semicolon)
Without adding a space, type this at the end: C:\Python27
Hit enter twice. Hit esc.
Use windows key + r to bring up the run dialog. Type in python and press enter.

Just a few comments:
Don't set PYTHONPATH if all you want is to get Python on the PATH. The PYTHONPATH environment variable tells Python where to look for modules to import. Setting it to C:\Python27\ will not accomplish anything useful, although it's probably harmless.
Modifying environment variables (including PATH) from the "Edit System Variables" has no effect on already running processes. This means you have to re-launch cmd.exe for the changes to work. A reboot, however, is not required.
When modifying the PATH, also add the Scripts subdirectory. Or, to put it in other words (and using the previous example): add ;C:\Python27;C:\Python27\Scripts. This will allow you to run scripts like easy_install, pip, virtualenv or sphinx from the command line - once you install those, that is. This is about as UNIX-y as it gets for Windows. (N.B. The Scripts subdirectory is not present after a clean install of Python, but will be created when needed.)
Don't put any additional Lib or DLL directory on the PATH. There's no need, and it might do harm.
If you have installed multiple versions of Python (which isn't all that uncommon) you might be better off not putting any of them on the PATH but instead create different shortcuts to cmd.exe for the different versions which set the PATH for each version. You might also be interested in PEP-397.

I am probably the most novice user here, I have spent six hours just to run python in the command line in Windows 8. Once I installed the 64-bit version, then I uninstalled it and replaced it with 32-bit version. Then, I tried most suggestions here, especially by defining path in the system variables, but still it didn't work.
Then I realised when I typed in the command line:
echo %path%
The path still was not directed to C:\python27. So I simply restarted the computer, and now it works.

You have to add the python executable in your SYSTEM PATH, do the following, My Computer > Properties > Advanced System Settings > Environment Variables > Then under system variables I create a new Variable called "PythonPath". In this variable I have "C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-foolder-on-the-path".

Here's one for for office workers using a computer shared by others.
I did put my user path in path and created the PYTHONPATH variables in my computer's PATH variable. Its listed under Environment Variables in Computer Properties -> Advanced Settings in Windows 7.
Example:
C:\Users\randuser\AppData\Local\Programs\Python\Python37
This made it so I could use the command prompt.
Hope this helped.

Seems like the python executable is not found in your PATH, which defines where it is looking for executables. See the official instructions for instructions on how to get the python executables in your PATH.

Add the python bin directory to your computer's PATH variable. Its listed under Environment Variables in Computer Properties -> Advanced Settings in Windows 7. It should be the same for Windows 8.

You need to add python to your PATH. I could be wrong, but Windows 7 should have the same cmd as Windows 8. Try this in the command line. Using setx permanently makes changes to you PATH. Note there are no equal signs, and quotes are used.
setx PATH "%PYTHONPATH%;C:\python27"
Set the c:\python27 to the directory of the python version you'd like to run from the typing python into the command prompt.

Just go with the command py. I'm running python 3.6.2 on windows 7 and it works just fine.
I removed all the python paths from the system directory and the paths don't show up when I run the command echo %path% in cmd. Python is still working fine.
I ran into this by accidentally pressing enter while typing python...
EDIT: I didn't mention that I installed python to a custom folder C:\Python\

When you add the python directory to the path (Computer > Properties > Advanced System Settings > Advanced > Environmental Variables > System Variables > Path > Edit), remember to add a semicolon, then make sure that you are adding the precise directory where the file "python.exe" is stored (e.g. C:\Python\Python27 if that is where "python.exe" is stored). Then restart the command prompt.

If you are working with command prompt and if you are facing the issue even after adding python path to system variable PATH.
Remember to restart the command prompt (cmde.exe).

I wanted to add a common problem that happens on installation. It is possible that the path installation length is too long. To avoid this change the standard path so that it is shorter than 250 characters.
I realized this when I installed the software and did a custom installation, on a WIN10 operation system. In the custom install, it should be possible to have Python added as PATH variable by the software

In my scenario, when I execute py -V it will show me the python version,
but python -V will show me Python was not found;
Answers above show us that we may need to set environment variable for python. But I already set the environment variable.
Finally, when I try to execute where.exe python command, it showed me with 2 python.exe, one of them is in path \AppData\Local\Microsoft\WindowsApps which is also set as environment, so I delete this python.exe file, then it worked.

None of these actually worked for me.
What you needed to do to really have Python recognized within it's path, is to download the latest version of it only from this website and not other website:
https://www.python.org/downloads/
But be careful while installing; the default installation is set not to add Python's path to the Environmental Variables in the Control Panel if you have a Windows computer, but you should change the setting so that the installation does it, and it will all be done by itself.

Related

How to use an already installed Python?

I have Plex installed on my PC, which relies on Python to run. I know nothing about Python, but I can see that it is running in Windows Task Manager. Now, I've got some other unrelated Python scripts that I need to run, but I have no idea how to find where Python is located on my system or how to access it.
According to How can I find where Python is installed on Windows?, I'm supposed to go to my Python interpreter to find out, but I have no idea what or where that is.
This is completely new to me. Could someone hold my hand and walk me through how I can run .py files?
Plex provides their own python libraries. This is built on Python 2.7. You do not need Python installed in order to run Plex and it's not recommended to use 2.7 at this point. You also cannot use the Python version provided by Plex, in the way you are hoping, as they don't include the executable.
Inside the installation directory of Plex you will see two files.
python27.dll
python27.zip
The contents of the zip folder are very close to the Lib folder from a standard installation of Python 2.7.
None of the provided solutions worked for you because python was not installed or available on your system path. Python can be installed by downloading it directly from Python (https://www.python.org/downloads/), executing the downloaded file, and following the prompts.
An alternative way to install Python is from the Microsoft Store, for Windows.
I will assume you are running on windows,so all you have to do is go to the Window's search option and search Python and you will there is python and then you just have to click it and click on open file location and then you will find the folder of it and your desired python version will be there.Then you just have to left click to go to the properties of it and click on open file location and you will find where you'r python is located and i will assume your python is most likely in the AppData\Local\Programes\Python folder :)
In windows: open the command prompt and type python, if it is set in your environment variables (which it asks while installation) it will open the python interpreter.
C:\WINDOWS\system32>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
for checking other files and modules, try on interpreter below.
>>> import sys
>>>
>>> print(sys.path)
To execute a python file. Simply open the command prompt and go to the location of the file and type python <name_of_python_file>
Python should be located at C:\Program Files\Python Are you trying to run py files through Command Prompt (CMD) or WindowsPowershell ? If yes then you can move to the directory, where your py files are located and just type the name of the py file in your terminal, to run it.
If you still cant find the directory, you can use a python interpreter and write:
(you can also open python in CMD by typing python then write the code below line by line,hitting enter after each line)
import os
import sys
print(os.path.dirname(sys.executable))
In Command Prompt(CMD) use cd path_name(f.example desktop) to move in directory, and cd .. to move back. Then you can us dir to see a list of the files in that directory , and simply type the name of your file for example python_file and hit enter to run it.
(Syntax might differ in WindowsPowershell)
None of the suggestions for finding where Python was installed worked.
As #tdelaney mentioned in the comments, even though Plex uses Python and it's running in my system, I don't actually have a standard install of it. I had to go download it.

Adding external library to Pycharm when using Python in ArcGIS [duplicate]

This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
So I have recently installed Python Version 2.7.5 and I have made a little loop thing with it but the problem is, when I go to cmd and type python testloop.py I get the error:
'python' is not recognized as an internal or external command
I have tried setting the path but no avail.
Here is my path:
C:\Program Files\Python27
As you can see, this is where my Python is installed. I don't know what else to do. Can someone help?
Try "py" instead of "python" from command line:
C:\Users\Cpsa>py
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have found the answer... click on the installer and check the box "Add python to environment variables" DO NOT uninstall the old one rather click on modify....Click on link for picture...
You need to add that folder to your Windows Path:
https://docs.python.org/2/using/windows.html Taken from this question.
If you want to see python version then you should use py -V instead of python -V
C:\Users\ghasan>py -V
Python 3.7.1
If you want to go to python's running environment then you should use py instead of python
C:\Users\ghasan>py
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Here you can run the python program as:
print('Hello Python')
Hello Python
Firstly, be sure where your python directory. It is normally in C:\Python27. If yours is different then change it from the below command.
If after you install it python still isn’t recognized, then in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27",
"User")
Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.
Type py -v instead of python -v in command prompt
i solved this by running CMD in administration mode, so try this.
Open CMD with administrative access(Right click then run as administrator) then type the following command there:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Replace My_python_lib with the folder name of your installed python like for me it was C:\python27.
Then to check if the path variable is set, type echo %PATH% you'll see your python part in the end. Hence now python is accessible.
From this tutorial
I have installed python 3.7.4. First, I tried python in my command prompt. It was saying that 'Python is not recognized command......'. Then I tried 'py' command and it works.
My sample command is:
py hacker.py
I have met same issue when I install Python, and it is resolved when I set a PATH in system, here are the steps.
Navigate to "Control Panel" -> "System"
Click "Advanced system settings" on the left
Click "Environment Variables"
Search and click "Path" variable
Click "Edit"
Add "C:\"to the environment variables field, if you are using Windows7, then separate it by a semicolon from the existing entry. If you are using Windows10, just simply click "New" to add.
Reopen the Command Prompt and try
enter image description here
From the Python docs, set the PATH like you did as above.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command dir C:\py* will probably tell you where it is installed; the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk
Use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the command
Then execute the Python command using the full path name to make sure that works.
Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.
If you uninstalled then re-installed, and running 'python' in CLI, make sure to open a new CMD after your installation for 'python' to be recognized. 'py' will probably be recognized with an old CLI because its not tied to any version.
Option 1 : Select on add environment var during installation
Option 2 : Go to C:\Users-> AppData (hidden file) -> Local\Programs\Python\Python38-32(depends on version installed)\Scripts
Copy path and add to env vars path.
For me this path worked : C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Scripts
It was a bit more confusing with the Python instructions once SQL Server 2019 was installed with Python. The actual path I find is as follows:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
Scripts run with an Execute command:
Declare #script nvarchar(max)=N'print(11-2)' execute sp_execute_external_script #language = N'Python', #script = #script
There is additional documentation in reference to SQL 2019's version of Python. There is a statement that recommends PIP be used only from a download of sqlmutils-x.x.x.zip located on git (https://www.github.com/Microsoft/sqlmutils) But there is a caveat. Currently this only works for R and not for Python (Anaconda and consequently pip). Python over SQL works but pip is not yet available. (11/25/2019)
Would be great to get an update when this occurs.

Trying to install tweepy to python in command prompt [duplicate]

This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
So I have recently installed Python Version 2.7.5 and I have made a little loop thing with it but the problem is, when I go to cmd and type python testloop.py I get the error:
'python' is not recognized as an internal or external command
I have tried setting the path but no avail.
Here is my path:
C:\Program Files\Python27
As you can see, this is where my Python is installed. I don't know what else to do. Can someone help?
Try "py" instead of "python" from command line:
C:\Users\Cpsa>py
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have found the answer... click on the installer and check the box "Add python to environment variables" DO NOT uninstall the old one rather click on modify....Click on link for picture...
You need to add that folder to your Windows Path:
https://docs.python.org/2/using/windows.html Taken from this question.
If you want to see python version then you should use py -V instead of python -V
C:\Users\ghasan>py -V
Python 3.7.1
If you want to go to python's running environment then you should use py instead of python
C:\Users\ghasan>py
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Here you can run the python program as:
print('Hello Python')
Hello Python
Firstly, be sure where your python directory. It is normally in C:\Python27. If yours is different then change it from the below command.
If after you install it python still isn’t recognized, then in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27",
"User")
Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.
Type py -v instead of python -v in command prompt
i solved this by running CMD in administration mode, so try this.
Open CMD with administrative access(Right click then run as administrator) then type the following command there:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Replace My_python_lib with the folder name of your installed python like for me it was C:\python27.
Then to check if the path variable is set, type echo %PATH% you'll see your python part in the end. Hence now python is accessible.
From this tutorial
I have installed python 3.7.4. First, I tried python in my command prompt. It was saying that 'Python is not recognized command......'. Then I tried 'py' command and it works.
My sample command is:
py hacker.py
I have met same issue when I install Python, and it is resolved when I set a PATH in system, here are the steps.
Navigate to "Control Panel" -> "System"
Click "Advanced system settings" on the left
Click "Environment Variables"
Search and click "Path" variable
Click "Edit"
Add "C:\"to the environment variables field, if you are using Windows7, then separate it by a semicolon from the existing entry. If you are using Windows10, just simply click "New" to add.
Reopen the Command Prompt and try
enter image description here
From the Python docs, set the PATH like you did as above.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command dir C:\py* will probably tell you where it is installed; the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk
Use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the command
Then execute the Python command using the full path name to make sure that works.
Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.
If you uninstalled then re-installed, and running 'python' in CLI, make sure to open a new CMD after your installation for 'python' to be recognized. 'py' will probably be recognized with an old CLI because its not tied to any version.
Option 1 : Select on add environment var during installation
Option 2 : Go to C:\Users-> AppData (hidden file) -> Local\Programs\Python\Python38-32(depends on version installed)\Scripts
Copy path and add to env vars path.
For me this path worked : C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Scripts
It was a bit more confusing with the Python instructions once SQL Server 2019 was installed with Python. The actual path I find is as follows:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
Scripts run with an Execute command:
Declare #script nvarchar(max)=N'print(11-2)' execute sp_execute_external_script #language = N'Python', #script = #script
There is additional documentation in reference to SQL 2019's version of Python. There is a statement that recommends PIP be used only from a download of sqlmutils-x.x.x.zip located on git (https://www.github.com/Microsoft/sqlmutils) But there is a caveat. Currently this only works for R and not for Python (Anaconda and consequently pip). Python over SQL works but pip is not yet available. (11/25/2019)
Would be great to get an update when this occurs.

'python' is not recognized as an internal or external command [duplicate]

This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
So I have recently installed Python Version 2.7.5 and I have made a little loop thing with it but the problem is, when I go to cmd and type python testloop.py I get the error:
'python' is not recognized as an internal or external command
I have tried setting the path but no avail.
Here is my path:
C:\Program Files\Python27
As you can see, this is where my Python is installed. I don't know what else to do. Can someone help?
Try "py" instead of "python" from command line:
C:\Users\Cpsa>py
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have found the answer... click on the installer and check the box "Add python to environment variables" DO NOT uninstall the old one rather click on modify....Click on link for picture...
You need to add that folder to your Windows Path:
https://docs.python.org/2/using/windows.html Taken from this question.
If you want to see python version then you should use py -V instead of python -V
C:\Users\ghasan>py -V
Python 3.7.1
If you want to go to python's running environment then you should use py instead of python
C:\Users\ghasan>py
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Here you can run the python program as:
print('Hello Python')
Hello Python
Firstly, be sure where your python directory. It is normally in C:\Python27. If yours is different then change it from the below command.
If after you install it python still isn’t recognized, then in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27",
"User")
Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.
Type py -v instead of python -v in command prompt
i solved this by running CMD in administration mode, so try this.
Open CMD with administrative access(Right click then run as administrator) then type the following command there:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
Replace My_python_lib with the folder name of your installed python like for me it was C:\python27.
Then to check if the path variable is set, type echo %PATH% you'll see your python part in the end. Hence now python is accessible.
From this tutorial
I have installed python 3.7.4. First, I tried python in my command prompt. It was saying that 'Python is not recognized command......'. Then I tried 'py' command and it works.
My sample command is:
py hacker.py
I have met same issue when I install Python, and it is resolved when I set a PATH in system, here are the steps.
Navigate to "Control Panel" -> "System"
Click "Advanced system settings" on the left
Click "Environment Variables"
Search and click "Path" variable
Click "Edit"
Add "C:\"to the environment variables field, if you are using Windows7, then separate it by a semicolon from the existing entry. If you are using Windows10, just simply click "New" to add.
Reopen the Command Prompt and try
enter image description here
From the Python docs, set the PATH like you did as above.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command dir C:\py* will probably tell you where it is installed; the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk
Use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the command
Then execute the Python command using the full path name to make sure that works.
Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.
If you uninstalled then re-installed, and running 'python' in CLI, make sure to open a new CMD after your installation for 'python' to be recognized. 'py' will probably be recognized with an old CLI because its not tied to any version.
Option 1 : Select on add environment var during installation
Option 2 : Go to C:\Users-> AppData (hidden file) -> Local\Programs\Python\Python38-32(depends on version installed)\Scripts
Copy path and add to env vars path.
For me this path worked : C:\Users\Username\AppData\Local\Programs\Python\Python38-32\Scripts
It was a bit more confusing with the Python instructions once SQL Server 2019 was installed with Python. The actual path I find is as follows:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
Scripts run with an Execute command:
Declare #script nvarchar(max)=N'print(11-2)' execute sp_execute_external_script #language = N'Python', #script = #script
There is additional documentation in reference to SQL 2019's version of Python. There is a statement that recommends PIP be used only from a download of sqlmutils-x.x.x.zip located on git (https://www.github.com/Microsoft/sqlmutils) But there is a caveat. Currently this only works for R and not for Python (Anaconda and consequently pip). Python over SQL works but pip is not yet available. (11/25/2019)
Would be great to get an update when this occurs.

How to run multiple Python versions on Windows

I had two versions of Python installed on my machine (versions 2.6 and 2.5). I want to run 2.6 for one project and 2.5 for another.
How can I specify which I want to use?
I am working on Windows XP SP2.
Running a different copy of Python is as easy as starting the correct executable. You mention that you've started a python instance, from the command line, by simply typing python.
What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.
Now, if you've installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.
What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.
The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.
Adding two more solutions to the problem:
Use pylauncher (if you have Python 3.3 or newer there's no need to install it as it comes with Python already) and either add shebang lines to your scripts;
#! c:\[path to Python 2.5]\python.exe - for scripts you want to be run with Python 2.5
#! c:\[path to Python 2.6]\python.exe - for scripts you want to be run with Python 2.6
or instead of running python command run pylauncher command (py) specyfing which version of Python you want;
py -2.6 – version 2.6
py -2 – latest installed version 2.x
py -3.4 – version 3.4
py -3 – latest installed version 3.x
Install virtualenv and create two virtualenvs;
virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]
virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]
for example
virtualenv -p c:\python2.5\python.exe c:\venvs\2.5
virtualenv -p c:\python2.6\python.exe c:\venvs\2.6
then you can activate the first and work with Python 2.5 like this
c:\venvs\2.5\activate
and when you want to switch to Python 2.6 you do
deactivate
c:\venvs\2.6\activate
From Python 3.3 on, there is the official Python launcher for Windows (http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonX to determine the wanted version of the interpreter also on Windows. See more details in my another comment or read the PEP 397.
Summary: The py script.py launches the Python version stated in #! or Python 2 if #! is missing. The py -3 script.py launches the Python 3.
As per #alexander you can make a set of symbolic links like below. Put them somewhere which is included in your path so they can be easily invoked
> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe
As long as c:\bin or where ever you placed them in is in your path you can now go
> python25
For example for 3.6 version type py -3.6.
If you have also 32bit and 64bit versions, you can just type py -3.6-64 or py -3.6-32.
install python
C:\Python27
C:\Python36
environment variable
PYTHON2_HOME: C:\Python27
PYTHON3_HOME: C:\Python36
Path: %PYTHON2_HOME%;%PYTHON2_HOME%\Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%\Scripts;
file rename
C:\Python27\python.exe → C:\Python27\python2.exe
C:\Python36\python.exe → C:\Python36\python3.exe
pip
python2 -m pip install package
python3 -m pip install package
I strongly recommend the pyenv-win project.
Thanks to kirankotari's work, now we have a Windows version of pyenv.
One easy way for this is that you can use
py -3.8 -m pip install virtualenv here -3.8 goes with your [version number]
After installing the virtualenv, you can create the virtual environment of your application using
py -3.8 -m virtualenv [your env name]
then cd to venv, enter activate
This would activate the python version you like.
Just change the version number to use a different python version.
When you install Python, it will not overwrite other installs of other major versions. So installing Python 2.5.x will not overwrite Python 2.6.x, although installing 2.6.6 will overwrite 2.6.5.
So you can just install it. Then you call the Python version you want. For example:
C:\Python2.5\Python.exe
for Python 2.5 on windows and
C:\Python2.6\Python.exe
for Python 2.6 on windows, or
/usr/local/bin/python-2.5
or
/usr/local/bin/python-2.6
on Windows Unix (including Linux and OS X).
When you install on Unix (including Linux and OS X) you will get a generic python command installed, which will be the last one you installed. This is mostly not a problem as most scripts will explicitly call /usr/local/bin/python2.5 or something just to protect against that. But if you don't want to do that, and you probably don't you can install it like this:
./configure
make
sudo make altinstall
Note the "altinstall" that means it will install it, but it will not replace the python command.
On Windows you don't get a global python command as far as I know so that's not an issue.
Here's a quick hack:
Go to the directory of the version of python you want to run
Right click on python.exe
Select 'Create Shortcut'
Give that shortcut a name to call by( I use p27, p33 etc.)
Move that shortcut to your home directory(C:\Users\Your name)
Open a command prompt and enter name_of_your_shortcut.lnk(I use p27.lnk)
cp c:\python27\bin\python.exe as python2.7.exe
cp c:\python34\bin\python.exe as python3.4.exe
they are all in the system path, choose the version you want to run
C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
The easiest way to run multiple versions of python on windows is described below as follows:-
1)Download the latest versions of python from python.org/downloads by selecting the relevant version for your system.
2)Run the installer and select Add python 3.x to the path to set path automatically in python 3 (you just have to click the checkbox). For python 2 open up your python 2 installer, select whatever preferences you want but just remember to set Add python.exe to path to Will be installed on local hard drive, Now just click next and wait for the installer to finish.
3)When both the installations are complete. Right click on my computer--Go to properties--Select advanced system settings--Go to environment variables--Click on new under System variables and add a new system variable with variable name as PY_PYTHON and set this variable value to 3. Now click on OK and you should be done.
4)Now to test this open the command prompt. Once you are in there type python or py, It should open up python3.
5)Now exit out of python3 by typing exit(). Now type py -2 it should open python 2.
If none of this works then restart the computer and if the problem still persists then uninstall everything and repeat the steps.
Thanks.
This is a simple and elegant solution to easily run 2 or more different versions of python without using scripts in Windows. Whatever the version of python, it will start from the Command prompt.
I have python versions 3.6.6 and 3.9. The Environment Variable paths are normal and were automatically added when each version of python was installed.
It's best to install python using the "all users" option. This way the python will simply install to:
C:\program files\python36
C:\program files\python39
Open each of these python folders and find the python.exe file. Copy and paste the python.exe file into those same folders. Then carefully rename the copies to:
python36.exe
python39.exe
Open and edit Environment Variables. Add 4 new User Variables.
C:\Program Files\Python36\Scripts
C:\Program Files\Python36\python36.exe
C:\Program Files\Python39\Scripts
C:\Program Files\Program39\python39.exe
Save and exit Environment Variables.
Open a new Command Prompt terminal window. To run one or the other version of python, type:
python36
python39
More versions of python can easily be added by repeating the same as shown above. Elegant and simple. Done.
Using a batch file to switch, easy and efficient on windows 7. I use this:
In the environment variable dialog (C:\Windows\System32\SystemPropertiesAdvanced.exe),
In the section user variables
added %pathpython% to the path environment variable
removed any references to python pathes
In the section system variables
removed any references to python pathes
I created batch files for every python installation (exmple for 3.4 x64
Name = SetPathPython34x64 !!! ToExecuteAsAdmin.bat ;-) just to remember.
Content of the file =
Set PathPython=C:\Python36AMD64\Scripts\;C:\Python36AMD64\;C:\Tcl\bin
setx PathPython %PathPython%
To switch between versions, I execute the batch file in admin mode.
!!!!! The changes are effective for the SUBSEQUENT command prompt windows OPENED. !!!
So I have exact control on it.
let's say if we have python 3.7 and python 3.6 installed.
they are respectively stored in following folder by default.
C:\Users\name\AppData\Local\Programs\Python\Python36
C:\Users\name\AppData\Local\Programs\Python\Python37
if we want to use cmd prompt to install/run command in any of the above specific environment do this:
There should be python.exe in each of the above folder.
so when we try running any file for ex. (see image1) python hello.py. we call that respective python.exe. by default it picks lower version of file. (means in this case it will use from python 3.6 )
image
so if we want to run using python3.7. just change the .exe file name. for ex. if I change to python37.exe and i want to use python3.7 to run hello.py
I will use python37 hello.py . or if i want to use python3.7 by default i will change the python.exe filename in python3.6 folder to something else . so that it will use python3.7 each time when I use only python hello.py
Shows your installed pythons
py -0
Uses version of python to do something
py -*version*
ex.
py -3.8 venv venv
Will create virtual environment in python 3.8
Note:
python -0
or
python -3.8
doesn't work, I assume it has to be "py"
You can create different python development environments graphically from Anaconda Navigator.
I had same problem while working with different python versions so I used anaconda navigator to create different python development environments and used different python versions in each environments.
Here is the help documentation for this.
https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/
Introduce more details based on the answer given by #Aman.
Define different environment variables for different python versions.
For example:
You have E:\python2\python.exe and E:\python3\python.exe at the same time.
Then you can set an environment variable %python2% for E:\python2\python.exe and %python2% for E:\python3\python.exe.
Finally, when you want to run python2 (or python3), you can enter %python2% (or %python3%) directly in command prompt.
Here is a solution:
First, install all versions which you want to run in your pc. https://www.python.org/
Second, create virtual environment with which python version you want to use.
"py [python_version] -m venv [vritual_environment_name]" example: "py -3.9 -m venv env"
Note: You don't need to run "pip install virtualenv"
Using the Rapid Environment Editor you can push to the top the directory of the desired Python installation. For example, to start python from the c:\Python27 directory, ensure that c:\Python27 directory is before or on top of the c:\Python36 directory in the Path environment variable. From my experience, the first python executable found in the Path environment is being executed. For example, I have MSYS2 installed with Python27 and since I've added C:\MSYS2 to the path before C:\Python36, the python.exe from the C:\MSYS2.... folder is being executed.
I thought this answer might be helpful to others having multiple versions of python and wants to use pipenv to create virtual environment.
navigate to the project directory, and run py -[python version] pip install pipenv, example: py -3.6 pip install pipenv
run pipenv --python [version] to create the virtual environment in the version of the python you desire. example: pipenv --python 3.6
run pipenv shell to activate your virtual environment.
Just call the correct executable

Categories