+2 votes
in Programming Languages by (37.5k points)
I have to run python codes that need the old version of some packages. I do not want to downgrade installed packages on my machine, so I want to create a virtual environment to run those codes. I will install the old version of the required packages in the virtual environment. How can I create a Python virtual environment?

1 Answer

+1 vote
by (233k points)
selected by
Best answer

The module that is used to create and manage virtual environments is called venv. Follow these steps to create a virtual environment and to activate it:

1. Install venv using the following command:

sudo apt install python3-venv 

2.  Choose a directory to create a virtual environment, and run the venv module with the directory path.

python3 -m venv virtual_environment_directory

This will create the virtual_environment_directory directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

3. Once a virtual environment is created, you may activate it using the following command:

source virtual_environment_directory/bin/activate