How to install virtual environment with specific python versions


When working on a Python project, it’s important to keep your dependencies isolated from other projects and from your system’s global Python installation. One way to achieve this is by using virtual environments. In this post, we’ll cover how to create and use virtual environments in Python.

What is a Virtual Environment?

A virtual environment is a self-contained directory tree that contains a Python installation and any additional packages you install. When you activate a virtual environment, it modifies your shell’s PATH environment variable to use the Python binary and packages from the virtual environment, rather than the global Python installation.

Using virtual environments has several benefits:

  • Isolation: Dependencies for one project won’t conflict with dependencies for another project.
  • Consistency: You can ensure that everyone working on the project is using the same versions of Python and packages.
  • Reproducibility: You can recreate the exact same environment on different machines.

Creating a Virtual Environment

First we discuss different tools to use:

  1. venv: This is a built-in module in Python 3.3 and later versions that allows you to create virtual environments in Python. It creates a new Python environment with its own site directories, which can be used to install and manage packages for specific projects. It’s simple, lightweight, and easy to use.
  2. virtualenv: This is a popular third-party tool that allows you to create isolated Python environments. It works with both Python 2 and 3 and allows you to create virtual environments with different Python versions, which can be useful for testing your code across different Python versions.
    Here we use venv.

Creating a virtual environment is easy. First, open a terminal or command prompt and navigate to the directory where you want to create the virtual environment. Then, run the following command:

python -m venv myenv

This will create a new virtual environment in a directory called myenv. You can replace myenv with any name you like.

If you need to create a virtual environment with a specific version of Python, you first need to install that version of Python on your system. You can use a package manager like pyenv to install multiple versions of Python on your system and switch between them easily. Once you have installed the desired version of Python, you can create a virtual environment with that version by specifying the full path to the Python interpreter in the python command:

/path/to/python3.10 -m venv myenv

This will create a new virtual environment called myenv with the specified version of Python.

Activating a Virtual Environment

Once you have created a virtual environment, you need to activate it before you can use it. To activate a virtual environment, run the following command:

source myenv/bin/activate

This will modify your shell’s PATH environment variable to use the Python binary and packages from the virtual environment. You should see the name of your virtual environment in your command prompt or terminal.

Once you have activated a virtual environment, any packages you install using pip will be installed into that environment, rather than the global Python installation.

Deactivating a Virtual Environment

When you’re done working in a virtual environment, you can deactivate it by running the following command:

deactivate

This will restore your shell’s PATH environment variable to its previous state, so that you’re using the global Python installation again.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC