How to install both Python 2.x and Python 3.x in Windows
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Installing Python 2 and Python 3 side by side on Windows is possible, but it should be treated as a legacy compatibility setup. Python 2 reached end of life on January 1, 2020, so new development should use Python 3 unless an older toolchain forces you to keep Python 2 around.
The safest strategy is to install each version in a separate directory and use the Windows Python Launcher, py, to select the version you want per command.
Install Each Version into a Separate Location
The two versions must not overwrite each other. A common layout is:
- Python 2.7 in
C:\Python27 - Python 3.x in its normal installer path or another clear location such as
C:\Python311
During installation, distinct directories matter more than the exact folder names. They prevent ambiguity and make troubleshooting much easier.
If the Python Launcher is available, it will detect installed interpreters and let you target them explicitly.
Checking What Is Already Installed
Open Command Prompt and inspect the current state:
py -0p is especially useful on Windows because it lists registered Python runtimes and the paths they point to.
If both versions are installed correctly, you should be able to run:
That is usually more reliable than depending on whichever interpreter happens to appear first in PATH.
Using the Python Launcher Instead of Fighting PATH
Many Windows issues come from trying to make python switch versions globally. It is usually better to keep PATH simple and use the launcher explicitly:
You can also install packages for a specific interpreter this way:
That avoids the common problem where pip points to one Python version while python points to another.
Managing Virtual Environments
For Python 3 projects, use venv:
Python 2 does not include venv, so older environments typically used virtualenv:
Even when both runtimes are installed globally, isolate project dependencies per environment. That keeps old Python 2 libraries from interfering with modern Python 3 work.
Choosing a Default Version
If you type python and Windows always launches the wrong version, the cause is almost always PATH ordering or the Microsoft Store alias. You can solve that by:
- disabling conflicting Windows App execution aliases
- adjusting
PATHorder - using
pyinstead of plainpython
If you want the launcher to prefer Python 3 by default, you can create a py.ini file in your user profile with a simple setting:
Then py launches Python 3 unless you override it with -2.
Choosing the Version Per Script
The Windows launcher also understands shebang lines, which is useful when old and new scripts live in the same machine.
Or for a legacy maintenance script:
That keeps the version choice close to the script instead of relying entirely on shell state.
Package Management Matters
Python 2 and Python 3 cannot share most site-packages safely. Always install packages against the interpreter you plan to use:
If one command fails because pip is missing, bootstrap or upgrade it from the matching interpreter rather than copying packages across directories.
Common Pitfalls
The biggest pitfall is adding both interpreters and both Scripts folders to PATH without a clear plan. That often leads to python, pip, and idle resolving to different versions.
Another problem is assuming Python 2 can be treated like a normal long-term dependency. It should be isolated to the exact legacy project that still needs it, because many modern packages no longer support it at all.
Developers also frequently forget that virtual environments are version-specific. A Python 3 environment cannot be turned into a Python 2 environment by editing activation scripts.
Finally, avoid vague commands such as just pip install ... in mixed setups. Use py -2 -m pip or py -3 -m pip so the target interpreter is explicit.
Summary
- Python 2 and Python 3 can coexist on Windows when installed into separate directories.
- Use the Windows Python Launcher,
py, to select the desired interpreter. - Prefer
py -2andpy -3over relying on globalPATHbehavior. - Create separate environments and package sets for each version.
- Keep Python 2 only for legacy maintenance, not new development.
Related reading
- How to install CPU version of tensorflow using conda
- How to install latest cuDNN to conda?
- How to install lxml on Ubuntu
- How to install multiple python packages at once using pip
- How to install PIL with pip on Mac OS?
- How to install pip with Python 3?
- how to install pip with yum on EC2
- How to install psycopg2 with pip on Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.