Python
Installation Guide
Windows
Python 2.x
Python 3.x

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.

Browse interview questions

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:

bat
1py -0p
2python --version
3where python
4where py

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:

bat
py -2 --version
py -3 --version

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:

bat
py -2 script.py
py -3 script.py

You can also install packages for a specific interpreter this way:

bat
py -2 -m pip install requests==2.27.1
py -3 -m pip install requests

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:

bat
py -3 -m venv .venv
.venv\Scripts\activate
python --version

Python 2 does not include venv, so older environments typically used virtualenv:

bat
1py -2 -m pip install virtualenv
2py -2 -m virtualenv py2-env
3py2-env\Scripts\activate
4python --version

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 PATH order
  • using py instead of plain python

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:

ini
[defaults]
python=3

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.

python
#! python3
print("Run this with Python 3")

Or for a legacy maintenance script:

python
#! python2
print "Run this with Python 2"

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:

bat
py -2 -m pip list
py -3 -m pip list

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 -2 and py -3 over relying on global PATH behavior.
  • Create separate environments and package sets for each version.
  • Keep Python 2 only for legacy maintenance, not new development.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.