How to run different python versions in cmd [duplicate] - python

This question already has answers here:
How to run multiple Python versions on Windows
(22 answers)
Closed 6 years ago.
How can I configure windows command dialog to run different python versions in it? For example when I type python2 it runs python 2.7 and when I type python3 it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal.

I also met the case to use both python2 and python3 on my Windows machine. Here's how i resolved it:
download python2x and python3x, installed them.
add C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts to environment variable PATH.
Go to C:\Python35 to rename python.exe to python3.exe, also to C:\Python27, rename python.exe to python2.exe.
restart your command window.
type python2 scriptname.py, or python3 scriptname.py in command line to switch the version you like.

Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\ as py.exe and pyw.exe by the installer. The installer also creates associations with .py and .pyw. Then add #!python3 or #!python2 as the first lline. No need to add anything to the PATH environment variable.
Update: Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .py extension. Then you can launch the script by simply typing the scriptname.py on the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon.
The py.exe looks for C:\PythonXX\python.exe where XX is related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27, and Python 3.3.3 installed into C:\Python33. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.

I would suggest using the Python Launcher for Windows utility that was introduced into Python 3.3. You can manually download and install it directly from the author's website for use with earlier versions of Python 2 and 3.
Regardless of how you obtain it, after installation it will have associated itself with all the standard Python file extensions (i.e. .py, .pyw, .pyc, and .pyo files). You'll not only be able to explicitly control which version is used at the command-prompt, but also on a script-by-script basis by adding Linux/Unix-y shebang #!/usr/bin/env pythonX comments at the beginning of your Python scripts.

Related

How to set python 3.x priority? [duplicate]

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC β†’ Right-click β†’ Click on Properties β†’ Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
πŸ‘‰ From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini πŸ‘‡
[defaults]
python=3.7
Console output with Administrator and User right πŸ’‘:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)

How can I make the "python" command in terminal, run python3 instead of python2?

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.
When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".
Is there a way to run Python 3 just using "python"?
It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.
If you are using Linux, add the following into into ~/.bashrc
alias python=python3
Restart the shell and type python and python3 should start instead of python2.
If you're using Windows then you can use the Python Launcher For Windows.
This will allow you to use the py command to select different python installations such as:
py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)
Similarly you can set a shebang in your python files as demonstrated below:
#! python3
print('Hello World!')
If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.
What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.
Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:
python3 your_file_name.py
I hope this help you.
Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.
According to PEP-394,
"for the time being, all distributions should ensure that python refers to the same target as python2".
On *nix systems, there are three links to executables of python command line interpreter named
python, python2 and python3 in directory /usr/bin. The python link points to python2 according to the PEP, but you can change it to point to python3 by creating a new link to python3 and renaming it to python. Also, you have to delete the old python link.
on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2

How Should I Set Default Python Version In Windows?

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC β†’ Right-click β†’ Click on Properties β†’ Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
πŸ‘‰ From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini πŸ‘‡
[defaults]
python=3.7
Console output with Administrator and User right πŸ’‘:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)

How to install both Python 2.x and Python 3.x in Windows

I do most of my programming in Python 3.x on Windows 7, but now I need to use the Python Imaging Library (PIL), ImageMagick, and wxPython, all of which require Python 2.x.
Can I have both Python 2.x and Python 3.x installed in Windows 7? When I run a script, how would I "choose" which version of Python should run it? Will the aforementioned programs be able to handle multiple versions of Python installed at once? I have searched for hours and hours for how to do this to no avail.
Thanks.
I found that the formal way to do this is as follows:
Just install two (or more, using their installers) versions of Python on Windows 7 (for me work with 3.3 and 2.7).
Follow the instuctions below, changing the parameters for your needs.
Create the following environment variable (to default on double click):
Name: PY_PYTHON
Value: 3
To launch a script in a particular interpreter, add the following shebang (beginning of script):
#! python2
To execute a script using a specific interpreter, use the following prompt command:
> py -2 MyScript.py
To launch a specific interpreter:
> py -2
To launch the default interpreter (defined by the PY_PYTHON variable):
> py
Resources
Documentation: Using Python on Windows
PEP 397 - Python launcher for Windows
What I did was download both 2.7.6 and 3.3.4. Python 3.3.4 has the option to add the path to it in the environment variable so that was done. So basically I just manually added Python 2.7.6.
How to...
Start > in the search type in environment select "Edit environment variables to your account"1
Scroll down to Path, select path, click edit.
Add C:\Python27;
so you should have paths to both versions of Python there, but if you don't this you can easily edit it so that you do..... C:\Python27;C:\Python33;
Navigate to the Python27 folder in C:\ and rename a copy of python.exe to python2.exe
Navigate to the Python34 folder in C:\ and rename a copy of python.exe to python3.exe
Test: open up commmand prompt and type python2 ....BOOM! Python 2.7.6. exit out.
Test: open up commmand prompt and type python3 ....BOOM! Python 3.4.3. exit out.
Note: (so as not to break pip commands in step 4 and 5, keep copy of python.exe in the same directory as the renamed file)
I have multiple versions in windows.
I just change the exe name of the version I'm not defaulting to.
python.exe --> python26.exe
pythonw.exe --> pythonw26.exe
As for package installers, most exe installers allow you to choose the python install to add the package too.
For manual installation check out the --prefix option to define where the package should be installed:
http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme
If you use Anaconda Python, you can easily install various environments.
Say you had Anaconda Python 2.7 installed and you wanted a python 3.4 environment:
conda create -n py34 python=3.4 anaconda
Then to activate the environment:
activate py34
And to deactive:
deactivate py34
(With Linux, you should use source activate py34.)
Links:
Download Anaconda Python
Instructions for environments
To install and run any version of Python in the same system follow my guide below.
For example say you want to install Python 2.x and Python 3.x on the same Windows system.
Install both of their binary releases anywhere you want.
When prompted do not register their file extensions and
do not add them automatically to the PATH environment variable
Running simply the command python the executable that is first met in PATH will be chosen for launch. In other words, add the Python directories manually. The one you add first will be selected when you type python. Consecutive python programs (increasing order that their directories are placed in PATH) will be chosen like so:
py -2 for the second python
py -3 for the third python etc..
No matter the order of "pythons" you can:
run Python 2.x scripts using the command: py -2 (Python 3.x functionality) (ie. the first Python 2.x installation program found in your PATH will be selected)
run Python 3.x scripts using the command: or py -3 (ie. the first Python 3.x installation program found in your PATH will be selected)
In my example I have Python 2.7.14 installed first and Python 3.5.3. This is how my PATH variable starts with:
PATH=C:\Program Files\Microsoft MPI\Bin\;C:\Python27;C:\Program Files\Python_3.6\Scripts\;C:\Program Files\Python_3.6\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared
...
Note that Python 2.7 is first and Python 3.5 second.
So running python command will launch python 2.7 (if Python 3.5 the same command would launch Python 3.5).
Running py -2 launches Python 2.7 (because it happens that the second Python is Python 3.5 which is incompatible with py -2).
Running py -3 launches Python 3.5 (because it's Python 3.x)
If you had another python later in your path you would launch like so: py -4. This may change if/when Python version 4 is released.
Now py -4 or py -5 etc. on my system outputs: Requested Python version (4) not installed or Requested Python version (5) not installed etc.
Hopefully this is clear enough.
Here's what you can do:
Install cmder.
Open and use Cmder as you would with you cmd terminal.
Use the command alias to create command aliases.
I did the following:
alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe
And that's it! ;-)
I actually just thought of an interesting solution. While Windows will not allow you to easily alias programs, you can instead create renamed batch files that will call the current program.
Instead of renaming the executable which will break a lot of thing including pip, create the file python2.bat in the same directory as the python2.exe. Then add the following line:
%~dp0python %*
What does this archaic syntax mean? Well, it's a batch script, (Windows version of bash). %~dp0 gets the current directory and %* will just pass all the arguments to python that were passed to the script.
Repeat for python3.bat
You can also do the same for pip and other utilities, just replace the word python in the file with pip or whathever the filename. The alias will be whatever the file is named.
Best of all, when added to the PATH, Windows ignores the extension so running
python3
Will launch the python3 version and and the command python2 will launch the python2 version.
BTW, this is the same technique Spyder uses to add itself to the path on Windows. :)
Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows
You can install multiple versions of Python one machine, and during setup, you can choose to have one of them associate itself with Python file extensions. If you install modules, there will be different setup packages for different versions, or you can choose which version you want to target. Since they generally install themselves into the site-packages directory of the interpreter version, there shouldn't be any conflicts (but I haven't tested this). To choose which version of python, you would have to manually specify the path to the interpreter if it is not the default one. As far as I know, they would share the same PATH and PYTHONPATH variables, which may be a problem.
Note: I run Windows XP. I have no idea if any of this changes for other versions, but I don't see any reason that it would.
What I have done on my own windows computer where I have Python 2.7 and Python 3.4 installed is I wrote a simple .bat file in the same directory as my Python.exe files. They look something like,
cmd /k "c:\python27\python.exe" %*
The %* allows you to add arguments (Python files) afterwards. I believe /k keeps the prompt open after it finishes running the script. Then I save that as python27.bat Then I go to my Python 3 directory and make a bat file there. Now in my command line I can write
Python27 helloworld.py
Or
Python34 helloworld.py
And they will run in their respective versions of Python. Make sure that c:\python27 and c:\python34 are in your environment variables.
I got my answer from here
I did this in three steps by following the instructions here: This is all taken directly from here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html. I'm currently running Python 2.x on Windows 8 and have Anaconda 4.2.13 installed.
1) First install the latest version of python:
conda create -n python3 python=3 ipykernel
2) Next activate python3
activate python3
3) Install the kernel:
python -m ipykernel install --user
If you have Python 3 installed and want to install 2, switch the 2 and the 3 above. When you open a new notebook, you can now choose between Python 2 or 3.
Check your system environment variables after installing Python, python 3's directories should be first in your PATH variable, then python 2.
Whichever path variable matches first is the one Windows uses.
As always py -2 will launch python2 in this scenario.
I have encountered that problem myself and I made my launchers in a .bat so you could choose the version you want to launch.
The only problem is your .py must be in the python folder, but anyway here is the code:
For Python2
#echo off
title Python2 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 23, 24, 25, 26)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
path = %PATH%;C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
For Python3
#echo off
title Python3 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 31, 32, 33, 34)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
cls
set path = %PATH%:C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
Save them as .bat and follow the instructions inside.
Install the one you use most (3.3 in my case) over the top of the other. That'll force IDLE to use the one you want.
Alternatively (from the python3.3 README):
Installing multiple versions
On Unix and Mac systems if you intend to install multiple versions of Python
using the same installation prefix (--prefix argument to the configure script)
you must take care that your primary python executable is not overwritten by the
installation of a different version. All files and directories installed using
"make altinstall" contain the major and minor version and can thus live
side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to
${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the
same prefix you must decide which version (if any) is your "primary" version.
Install that version using "make install". Install all other versions using
"make altinstall".
For example, if you want to install Python 2.6, 2.7 and 3.3 with 2.7 being the
primary version, you would execute "make install" in your 2.7 build directory
and "make altinstall" in the others.
I just had to install them. Then I used the free (and portable) soft at http://defaultprogramseditor.com/ under "File type settings"/"Context menu"/search:"py", chose .py file and added an 'open' command for the 2 IDLE by copying the existant command named 'open with IDLE, changing names to IDLE 3.4.1/2.7.8, and remplacing the files numbers of their respective versions in the program path. Now I have just to right click the .py file and chose which IDLE I want to use. Can do the same with direct interpreters if you prefer.
I use a simple solution to switch from a version to another version of python, you can install all version you want. All you have to do is creating some variable environment.
In my case, I have installed python 2.7 and python 3.8.1, so I have created this environment variables:
PYTHON_HOME_2.7=<path_python_2.7>
PYTHON_HOME_3.8.1=<path_python_3.8.1>
PYTHON_HOME=%PYTHON_HOME_2.7%
then in my PATH environment variable I put only %PYTHON_HOME% and %PYTHON_HOME%\Scripts. In the example above I'm using the version 2.7, when I want to switch to the other version I have only to set the PYTHON_HOME=%PYTHON_HOME_3.8.1%.
I use this method to switch quickly from a version to another also for JAVA, MAVEN, GRADLE,ANT, and so on.
Only Works if your running your code in your Python IDE
I have both Python 2.7 and Python 3.3 installed on my windows operating system. If I try to launch a file, it will usually open up on the python 2.7 IDE. How I solved this issue, was when I choose to run my code on python 3.3, I open up python 3.3 IDLE(Python GUI), select file, open my file with the IDLE and save it. Then when I run my code, it runs to the IDLE that I currently opened it with. It works vice versa with 2.7.
I have installed both python 2.7.13 and python 3.6.1 on windows 10pro and I was getting the same "Fatal error" when I tried pip2 or pip3.
What I did to correct this was to go to the location of python.exe for python 2 and python 3 files and create a copy of each, I then renamed each copy to python2.exe and python3.exe depending on the python version in the installation folder. I therefore had in each python installation folder both a python.exe file and a python2.exe or python3.exe depending on the python version.
This resolved my problem when I typed either pip2 or pip3.
If you can't get anything else to work, open an interpreter in whichever version you choose (I prefer using iPython) and:
import subprocess
subprocess.call('python script.py -flags')
This uses whichever python version you are currently operating under. Works fine for a single script, but will quickly get out of hand if there are lots of scripts you run, in which case you can always make a batch file with all of these calls inside. Not the most elegant answer, but it works.
Is there a way to make aliases for different python version a la Linux?

Can I install Python 3.x and 2.x on the same Windows computer?

I'm running Windows and the shell/OS automatically runs Python based on the registry settings when you run a program on the command line. Will this break if I install a 2.x and 3.x version of Python on the same machine?
I want to play with Python 3 while still being able to run 2.x scripts on the same machine.
The official solution for coexistence seems to be the Python Launcher for Windows, PEP 397 which was included in Python 3.3.0. Installing the release dumps py.exe and pyw.exe launchers into %SYSTEMROOT% (C:\Windows) which is then associated with py and pyw scripts, respectively.
In order to use the new launcher (without manually setting up your own associations to it), leave the "Register Extensions" option enabled. I'm not quite sure why, but on my machine it left Py 2.7 as the "default" (of the launcher).
Running scripts by calling them directly from the command line will route them through the launcher and parse the shebang (if it exists). You can also explicitly call the launcher and use switches: py -3 mypy2script.py.
All manner of shebangs seem to work
#!C:\Python33\python.exe
#!python3
#!/usr/bin/env python3
as well as wanton abuses
#! notepad.exe
Here's my setup:
Install both Python 2.7 and 3.4 with the windows installers.
Go to C:\Python34 (the default install path) and change python.exe to python3.exe
Edit your environment variables to include C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;
Now in command line you can use python for 2.7 and python3 for 3.4.
From version 3.3 Python introduced Launcher for Windows utility https://docs.python.org/3/using/windows.html#python-launcher-for-windows.
So to be able to use multiple versions of Python:
install Python 2.x (x is any version you need)
install Python 3.x (x is any version you need also you have to have one version 3.x >= 3.3)
open Command Prompt
type py -2.x to launch Python 2.x
type py -3.x to launch Python 3.x
You can have both installed.
You should write this in front of your script:
#!/bin/env python2.7
or, eventually...
#!/bin/env python3.6
Update
My solution works perfectly with Unix, after a quick search on Google, here is the Windows solution:
#!c:/Python/python3_6.exe -u
Same thing: in front of your script.
Here is a neat and clean way to install Python2 & Python3 on windows.
https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a
My case: I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work under process i didn't wanted to mess my Python3 installation. And, i needed Python2 only for Apache cassandra.
So i took following steps:
Downloaded & Installed Python2.
Added Python2 entries to classpath (C:\Python27;C:\Python27\Scripts)
Modified python.exe to python2.exe (as shown in image below)
Now i am able to run both. For Python 2(python2 --version) & Python 3 (python --version).
So, my Python3 installation remained intact.
I'm using 2.5, 2.6, and 3.0 from the shell with one line batch scripts of the form:
:: The # symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
#c:\programs\pythonX.Y\python.exe %*
Name them pythonX.Y.bat and put them somewhere in your PATH. Copy the file for the preferred minor version (i.e. the latest) to pythonX.bat. (E.g. copy python2.6.bat python2.bat.) Then you can use python2 file.py from anywhere.
However, this doesn't help or even affect the Windows file association situation. For that you'll need a launcher program that reads the #! line, and then associate that with .py and .pyw files.
When you add both to environment variables there will a be a conflict because the two executable have the same name: python.exe.
Just rename one of them. In my case I renamed it to python3.exe.
So when I run python it will execute python.exe which is 2.7
and when I run python3 it will execute python3.exe which is 3.6
Here you go...
winpylaunch.py
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
# #!/usr/bin/python
# #!C:\Python26\python.exe
# # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
# HKEY_CLASSES_ROOT\Python.File\shell\open\command
# (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#
import subprocess
import sys
USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""
if __name__ == "__main__":
if len(sys.argv) > 1:
script = sys.argv[1]
args = sys.argv[2:]
if script.endswith(".py"):
interpreter = "python.exe" # Default to wherever it is found on the path.
lines = open(script).readlines()
for line in lines:
if line.startswith("#!") and line.find(".exe") != -1:
interpreter = line[2:].strip()
break
process = subprocess.Popen([interpreter] + [script] + args)
process.wait()
sys.exit()
print(USAGE)
I've just knocked this up on reading this thread (because it's what I was needing too). I have Pythons 2.6.1 and 3.0.1 on both Ubuntu and Windows. If it doesn't work for you post fixes here.
Try using Anaconda.
Using the concept of Anaconda environments, let’s say you need Python 3 to learn programming, but you don’t want to wipe out your Python 2.7 environment by updating Python. You can create and activate a new environment named "snakes" (or whatever you want), and install the latest version of Python 3 as follows:
conda create --name snakes python=3
Its simpler than it sounds, take a look at the intro page here: Getting Started with Anaconda
And then to handle your specific problem of having version 2.x and 3.x running side by side, see:
Managing Python Versions with Anaconda
Switching between Python 2 and Python 3 environments
As far as I know Python runs off of the commandline using the PATH variable as opposed to a registry setting.
So if you point to the correct version on your PATH you will use that. Remember to restart your command prompt to use the new PATH settings.
The Python installation normally associates .py, .pyw and .pyc files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type python scriptname.py, just scriptname.py will do).
If you want to manually change this association, you can edit these keys in the Windows registry:
HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command
Python Launcher
People have been working on a Python launcher for Windows: a lightweight program associated with .py and .pyw files which would look for a "shebang" line (similar to Linux et al) on the first line, and launch Python 2.x or 3.x as required. See "A Python Launcher for Windows" blog post for details.
Here is how to run Python 2 and 3 on the same machine
install Python 2.x
install Python 3.x
Start Powershell
Type Python -2 to launch Python 2.x
Type Python -3 to launch Python 3.x
The Python Launcher for Windows was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:
Python Launcher for Windows
Easy-peasy ,after installing both the python versions add the paths to the environment variables ;see. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown and . Now in cmd type python2 or python3 to use your required version see .
I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.
If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select "open with" / "choose program". On the dialog that appears, select or browse to the version of python you want to use by default, and check the "always use this program to open this kind of file" checkbox.
You should make sure that the PATH environment variable doesn't contain both python.exe files ( add the one you're currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files .
Aside from that , I don't see why not .
P.S : I have 2.6 installed as my "primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .
Before I courageously installed both simultaneously, I had so many questions. If I give python will it go to py3 when i want py2? pip/virtualenv will happen under py2/3?
It seems to be very simple now.
Just blindly install both of them. Make sure you get the right type(x64/x32).
While/after installing make sure you add to the path to your environment variables.
[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")
Replace the x in the command above to set the path.
Then go to both the folders.
Navigate to
python3.6/Scripts/
and rename pip to pip3.
If pip3 already exists delete the pip. This will make sure that just pip will run under python2. You can verify by:
pip --version
In case you want to use pip with python3 then just use
pip3 install
You can similarly do the same to python file and others.
Cheers!
I had the same problem where I wanted to use python3 for most work but IDA pro required python2. SO, here's what I did.
I first created 3 variables in the user environment variable as follows:
PYTHON_ACTIVE : This is initially empty
HOME_PYTHON27 : Has a path to a folder where Python 2 is installed. Eg. ";/scripts;"
HOME_PYTHON38 : Similar to python 2, this variable contains a path to python 3 folders.
Now I added
%PYTHON_ACTIVE%
to PATH variable. So, basically saying that whatever this "PYTHON_ACTIVE" contains is the active python. We programmatically change the contains of "PYTHON_ACTIVE" to switch python version.
Here is the example script:
:: This batch file is used to switch between python 2 and 3.
#ECHO OFF
set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "
IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)
IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)
PAUSE
This script takes python version as input and accordingly copies HOME_PYTHON27 or HOME_PYTHON38 to PYTHON_ACTIVE. Thus changing the global Python version.
I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.
I am just starting out with python now. I'm reading Zed Shaw's book "Learn Python the Hard Way" which requires python version 2.x but am also taking a class that requires python 3.x
So here is what I did.
Download python 2.7
run power shell (should already be installed on windows)
run python IN POWERSHELL (if it doesn't recognize then go to step 4)
Only if powershell doesn't recognize python 2.7 type in the following:
"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")"
(no outside quotes)
Now type python and you should see it say python 2.7 blah blah blah
NOW for python 3.x
Simple, python 3.x download comes with python for windows app. SO simply pin the Python for Windows app to your task bar, or create shortcut to the desktop and you are done!
Open Python for Windows for 3.x
Open Powershell for python 2.x
I hope this helps!
Hmm..I did this right now by just downloading Python 3.6.5 for Windows at https://www.python.org/downloads/release/python-365/ and made sure that the launcher would be installed. Then, I followed the instructions for using python 2 and python 3. Restart the command prompt and then use py -2.7 to use Python 2 and py or py -3.6 to use Python 3. You can also use pip2 for Python 2's pip and pip for Python 3's pip.

Categories

Resources