Home » Create virtual environments with another Python version

Create virtual environments with another Python version

  • by
  • 2 min read
Tags:

Nothing more frustrating in a data science project than a library that doesn’t work in your particular Python version. Sometimes, you just need to install a virtual environment with another Python version.

First of all, if you are a venv user, I hate to break it to you. There’s no straightforward way to create a virtual environment with another Python version using venv. For this reason, you’ll have to install virtualenv.

But first…

What is the difference between venv and virtualenv?

  • venv is a package that comes with Python 3. In other words: you don’t need to install an extra package to create virtual environments.
  • On the other hand, virtualenv is library that you can download via pip install virtualenv.

Furthermore, according to the virtualenv documentation, venv is…

  • is slower (by not having the app-data seed method),
  • is not as extendable,
  • cannot create virtual environments for arbitrarily installed python versions (and automatically discover these),
  • is not upgrade-able via pip,
  • does not have as rich programmatic API (describe virtual environments without creating them).

FOLLOW THE GRASP ON LINKEDIN

Curated pieces about data, analytics, machine learning, and artificial intelligence. No bullshit, no high-level promotional stuff trying to sell you a data strategy, no robots coming for your jobs. Just news, opinions and blogs from experts in tech and academics.

You won’t regret it. Trust me.

Virtual environments with other Python version

To create a virtual environment with another Python version, you have to take the following steps.

  1. Download the Python version that you need, e.g. Python 3.6
  2. Install the Python executable. I recommend a custom installation. There’s no need to add it to PATH.
  3. Run Virtual Studio Code (or any other editor or terminal). Windows: If the installation directory is within Program Files, run it as an Administrator.
  4. Install virtualenv in your main Python version via pip install virtualenv
  5. Create the virtual environment with virtualenv, and specify the -p parameter.
py -m virtualenv -p=<your_python_executable> <virtual_environment_directory>

If your directory contains spaces, wrap it in double quotes. Like this:

py -m virtualenv -p="C:\Program Files\Python36\python.Exe" .virtenv

Good luck!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Leave a Reply

Your email address will not be published. Required fields are marked *