[Python] Create and manage a virtual environment (venv)

table of contents
Hello, this is inusuki from the System Solutions Department
This time we will create a virtual environment using Python's venv
What is venv?
venv is a module added in version 3.3 for creating lightweight Python virtual environments on top of the base Python
Using virtual environments, you can install Python packages on a project-by-project basis, rather than system-wide
This means you can use different versions and packages for each project, and launch other projects without having to worry about dependency impacts
For creating virtual environments from version 3.5 onwards, it is recommended to use venv (as of August 2023)
Try making it yourself
Now let's try it out
If you are using version 3.3 or later, this is a standard feature, so no additional installation is required
This time, I am using Python 3.11.0 on Windows
python -V Python 3.11.0
Creating a virtual environment
For convenience, create a directory called test_dir and create a virtual environment in it
The command to create a venv (python -m venv) is the same on Linux
To simplify the management of virtual environments, we will create one named myenv1
mkdir test_dir cd test_dir python -m venv myenv1
Some columns have been hidden to make it easier to see, but the contents of venv are as follows.
Since we will only be focusing on usage this time, we will not go into details, but these are important directories/files for configuring a virtual environment, so please do not make any changes lightly.
ls myenv1 Directory: C:\Users\ichik\test_dir\myenv1 Mode LastWriteTime Name ---- ------------- ---- d---- 2023/08/28 13:30 Include d---- 2023/08/28 13:30 Lib d---- 2023/08/28 13:30 Scripts -a---- 2023/08/28 13:30 pyvenv.cfg
Activating the virtual environment
Activate the virtual environment with the following command:
After execution, if the environment name is displayed at the beginning of the prompt, activation was successful
.\myenv1\Scripts\Activate.ps1 (myenv1) PS C:\Users\ichik\test_dir>
*The commands differ depending on the execution environment, so please refer to the table below
| platform | shell | Command to activate the virtual environment |
|---|---|---|
| POSIX | bash/zsh | $ source |
| fish | $ source |
|
| csh/tcsh | $ source |
|
| PowerShell | $ |
|
| Windows | cmd.exe | C:\> |
| PowerShell | PS C:\> |
While the environment name is displayed, the Python in the virtual environment will be executed instead of the base Python
Note that when you create a venv in the standard way, the Python version of the virtual environment is the same as the base Python version
python -V Python 3.11.0
As mentioned above, the packages in the virtual environment are independent, so you can simply install them as you like
Disabling the virtual environment
Deactivate the virtual environment with the following command:
(myenv1) PS C:\Users\ichik\test_dir> deactivate
If you no longer need the virtual environment, you can delete the directory
ex) I want to copy and reuse a virtual environment
For example, if you are using it on a different PC or sharing it with others for development, you would not copy and reuse the venv itself
Create a list of packages and share them along with the source code using git or similar
You can output a list of packages with the following command:
(myenv1) PS C:\Users\ichik\test_dir> python -m pip freeze > requirements.txt
Next, run the following command in the newly launched virtual environment to install the packages all at once
(myenv1) PS C:\Users\ichik\test_dir> python -m pip install -r requirements.txt
ex2) I want to start a virtual environment with a different version (Windows)
You can also install any version to launch a virtual environment with a different version
First, check the version you have installed
py --list -V:3.11 * Python 3.11 (64-bit) -V:3.9 Python 3.9 (64-bit)
This time we will start a new project using Python 3.9
py -3.9 -m venv pyenv39 .\pyenv39\Scripts\Activate.ps1
The virtual environment has been launched with a different version than the base!
(pyenv39) PS C:\Users\ichik\test_dir> python -V Python 3.9.13
Then, you can run the program in a different version by referencing the source code and requirements.txt and running pip install
summary
- venv is a module added in version 3.3 for creating lightweight Python virtual environments on top of the base Python
- Different projects can use different versions and packages
- You can launch other projects without having to worry about dependencies
Thank you for viewing
4