Setting Up Python 2 and Python 3 Side by Side on Windows
To work with both Python 2 and Python 3 simultaneously on a Windows system, follow these steps:
- Download and install Python 2.x and Python 3.x.
- Configure environment variables. Add the installation directories and their Scripts subdirectories to the PATH user variable, separated by semicolons.
- Rename python.exe to python2.exe and python3.exe in their respective installation directories. Similarly, rename pythonw.exe to pythonw2.exe and pythonw3.exe.
- Upgrade pip for both versions by executing the following commands in Command Prompt:
python2 -m pip install --upgrade pip --force-reinstall python3 -m pip install --upgrade pip --force-reinstall
- Verify the installation by typing
python2orpython3in the command prompt to access the respective environments. Usepip2 -Vandpip3 -Vto check pip versions. Install packages withpip2 install package_nameorpip3 install package_name.
Detailed Instructions
1. Download and Install Python 2.x and Python 3.x
- Visit https://www.python.org/downloads/ and download the appropriate Windows installers for both versions.
- Run the installers with default settings. Note the installation directories (e.g., D:\Python27 and D:\Python35).
- During Python 3 installation, you may check "Add Python to PATH" to automatically configure environment variables.
2. Configure Environment Variables
- Open System Properties > Environment Variables.
- Edit the PATH variable to include both Python installations and their Scripts directories:
D:\Python27\Scripts;D:\Python27; D:\Python35\Scripts;D:\Python35;
3. Rename Executable Files
- In the Python 2 directory, rename python.exe to python2.exe and pythonw.exe to pythonw2.exe.
- In the Python 3 directory, rename python.exe to python3.exe and pythonw.exe to pythonw3.exe.
- Note: python.exe opens a console window, while pythonw.exe runs without a console (useful for GUI applications).
- In scripts, specify the interpreter with
#! D:\python27\python2.exefor Python 2 or#! D:\python35\python3.exefor Python 3.
4. Upgrade pip
Ensure both Python versions have their own pip installation by running the upgrade commands for each version:
python2 -m pip install --upgrade pip --force-reinstall python3 -m pip install --upgrade pip --force-reinstall
5. Verification
Test the setup by running:
python2 --versionandpython3 --versionto confirm correct versions.pip2 --versionandpip3 --versionto check pip versions.- Execute scripts with
python2 script.pyorpython3 script.py.