I am always forgetting how to do this and so I thought I would throw this quick write-up together about how you can get Python 2 and 3 to play nicely together on Windows 7 and higher.
Edit: So be sure to let me know in the comments section below if this article helps you or if you run into some kind of issues! I am mostly running Python 3 on Mac and Linux these days but I definitely want to keep this post up to date with the proper way to do this.
To run Python 2 and 3 side by side on Windows
-
Install Python 2 - 64bit with default options (DO NOT check the option to add anything to the path)
-
Install Python 3 - 64bit with default options (DO NOT check the option to add anything to the path)
Python 2 and 3 will be installed with their default locations and you will notice that typing python
at the command line will now do nothing. This is fine.
The installers will install the Python launcher, which is two executables into your Windows directory:
Python 2
To start the Python 2 interpreter
py
To run Python 2 pip
py -m pip list
To install Python 2 module
py -m pip install flask
To run Python 2 script
py main.py
To get version number
py --version
Python 3
To start the Python 3 interpreter
py -3
To run Python 3 pip
py -3 -m pip list
To install Python 3 module
py -3 -m pip install flask
To run Python 3 script
py -3 main.py
To get version number
py -3 --version
Create a virtual environment on the CLI with Python 3
# first install it
py -3 -m pip install virtualenv
# then create it
py -3 -m virtualenv venv
# activate it
venv\Scripts\activate.bat
Letting py
determine the proper interpreter to run
use #!python2
or #!python3
at the top of your script to let py.exe
determine whether to launch Python 2 or Python 3. Then you can launch either with something like py script.py
Great resources that helped me solve this issue
How do I run python 2 and 3 in windows 7?
How to use pip with python 3.4 on windows?
Comments
comments powered by Disqus