Installing Multiple Python Versions on Windows Using Virtualenv

As a Python developer, you‘ve likely encountered situations where you need to work on multiple projects that require different versions of Python or have conflicting package dependencies. Maybe one of your projects uses Python 2.7 and relies on an older version of a certain library, while another project requires Python 3.x and the latest version of that same library. How can you manage these differing requirements on the same machine without things getting messy?

The answer is to use virtual environments. Virtual environments allow you to create isolated "spaces" for your Python projects, each with its own Python binary and set of installed packages. This way, you can work on multiple projects with varying Python and package versions without them interfering with each other.

In this guide, we‘ll walk through the process of installing multiple Python versions on Windows and using the popular virtualenv tool to create and manage virtual environments for your projects. By the end, you‘ll be able to easily switch between different Python versions and keep your projects‘ dependencies neatly separated. Let‘s get started!

What is Virtualenv?

Virtualenv is a tool used to create isolated Python environments. It allows you to create a folder which contains all the necessary executables and packages to use a specific Python version, without affecting other Python projects or the system-wide Python installation.

When you activate a virtual environment, any packages you install or upgrade will be confined to that environment, leaving your system-wide Python untouched. This is especially useful when you need to work on multiple projects with conflicting dependencies or different Python versions.

Some key benefits of using virtualenv:

  • You can have multiple different Python versions installed and switch between them easily for different projects
  • You can avoid conflicts between packages required by different projects
  • You can experiment with new packages or package versions without fear of breaking your system-wide Python installation
  • You can easily replicate an environment on another machine by sharing the project‘s requirements.txt file

Now that we understand what virtualenv is and why it‘s useful, let‘s go through the steps to set it up on Windows.

Step 1: Install Virtualenv

First, make sure you have Python installed system-wide on your Windows machine. You can download the latest version of Python from the official website: https://www.python.org/downloads/

Once you have Python installed, open up a command prompt and run the following command to install virtualenv using pip:

pip install virtualenv

Pip is a package installer for Python that should be included in your Python installation by default. This command will fetch the virtualenv package from the Python Package Index and install it system-wide.

Step 2: Download and Install Multiple Python Versions

Next, we‘ll download and install the different versions of Python we want to use in our virtual environments.

Let‘s say we want Python 2.7 and Python 3.9 in addition to our system Python. Go to the Python downloads page (https://www.python.org/downloads/) and download the Windows x86-64 executable installers for those versions.

When you run the installers, make sure to check the option to "Add Python X.X to PATH" – this will ensure the python.exe for each version is available on your system PATH so virtualenv can find it later.

I recommend installing each Python version to a different directory to keep things organized, e.g.:

  • Python 2.7 to C:\Python27
  • Python 3.9 to C:\Python39

Once the installers finish, you should have multiple Python versions available on your machine. You can verify this by running the following in a new command prompt:

python --version
python2 --version 
python3 --version

You should see the version numbers printed out for each Python executable found on your PATH.

Step 3: Create Virtual Environments

Now we‘re ready to create virtual environments for our different Python versions using the virtualenv command.

Open up a new command prompt and navigate to the directory where you want to create your virtual environments. For example:

cd C:\Users\YourName\PythonProjects

To create a virtual environment with Python 2.7, run:

virtualenv venv27 -p C:\Python27\python.exe

This will create a new folder called "venv27" containing a Python 2.7 virtual environment. The -p argument specifies the path to the Python executable to use.

Similarly, to create a virtual environment with Python 3.9:

virtualenv venv39 -p C:\Python39\python.exe 

You can use any names you want for the virtual environment folders – just make sure to use something descriptive so you remember which Python version each one contains.

Step 4: Activate a Virtual Environment

To start using a virtual environment, you need to activate it. This will modify your shell‘s PATH so that running python and pip will use the virtual environment‘s executables instead of the system-wide ones.

To activate the Python 2.7 environment, run:

venv27\Scripts\activate

You should see the environment name in parentheses prepended to your command prompt, indicating it‘s active:

(venv27) C:\Users\YourName\PythonProjects> 

Now if you run python –version, you‘ll see it‘s using the Python 2.7 executable from your virtual environment.

To switch to the Python 3.9 environment, first deactivate the current environment:

deactivate

Then activate the other environment:

venv39\Scripts\activate
(venv39) C:\Users\YourName\PythonProjects>

And now python –version will show Python 3.9.

Step 5: Install Packages in Virtual Environments

With your virtual environment active, you can use pip to install any packages needed for your project. For example:

(venv39) C:\Users\YourName\PythonProjects> pip install numpy pandas matplotlib 

This will install the latest versions of NumPy, Pandas and Matplotlib into your active virtual environment. Other projects can have their own versions of these packages without interfering.

You can also use a requirements.txt file to install a specific set of package versions:

(venv39) C:\Users\YourName\PythonProjects> pip install -r requirements.txt

Where requirements.txt lists the packages and versions your project depends on.

It‘s a good practice to "freeze" your environment‘s package state into a requirements.txt file that you can commit to version control and share with others to recreate the same environment:

(venv39) C:\Users\YourName\PythonProjects> pip freeze > requirements.txt

Bonus: Configuring PyCharm

If you‘re using PyCharm as your Python IDE, you‘ll probably want to configure it to use the appropriate virtual environment and interpreter for each project.

To do this, open your project in PyCharm and go to File > Settings. Under Project > Python Interpreter, click the gear icon and select "Add". In the dialog that appears, select "Existing Environment" and browse to the virtual environment folder you created for the project, selecting the python.exe file inside the Scripts folder.

PyCharm will automatically detect the packages installed in that virtual environment and use them for things like auto-completion and import resolution. You can install additional packages from within PyCharm by clicking the + button in the Python Interpreter settings.

Troubleshooting Common Issues

Here are a few common problems you might encounter when working with virtualenv and how to resolve them:

  • Virtualenv command not found: Make sure you‘ve installed virtualenv successfully using pip. Check that your system PATH includes the directory where virtualenv was installed (usually the Python\Scripts\ folder).

  • Activation script not found: Check that you‘re running the activate script from the correct location within your virtual environment folder (e.g. venv39\Scripts\activate). Ensure the activate script exists.

  • Packages not found after activation: Make sure you‘ve installed the necessary packages in your virtual environment using pip while the environment was active. Packages installed system-wide will not be available in virtual environments by default.

  • Wrong Python version in virtual environment: Double check the path to the Python executable you specified when creating the virtual environment with the -p argument. Make sure it points to the correct Python installation.

If you encounter any other issues, the virtualenv documentation (https://virtualenv.pypa.io/) and Python community forums are great resources to find solutions. Don‘t hesitate to search for your specific error message or problem – chances are someone else has encountered it before!

Conclusion

In this guide, we‘ve learned how to install and manage multiple Python versions on Windows using virtualenv. By leveraging virtual environments, you can work on projects with different Python and package requirements simultaneously without conflicts.

The key steps are:

  1. Install virtualenv using pip
  2. Download and install the needed Python versions
  3. Create virtual environments for each Python version using virtualenv
  4. Activate the virtual environment you want to use
  5. Install packages in the virtual environment using pip

We‘ve also seen how to configure PyCharm to use the correct virtual environment for each project, and covered some common issues and how to troubleshoot them.

Using virtual environments might seem like a bit of extra work at first, but the benefits of being able to easily switch between Python versions and keep project dependencies isolated are well worth it in the long run. It‘s a best practice that will make your development workflow smoother and more organized.

I encourage you to start using virtualenv for your Python projects if you‘re not already. It‘s a powerful tool that every Python developer should have in their toolkit. Happy coding!

Similar Posts