[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 base Python.
By using a virtual environment, you can install Python packages on a project-by-project basis instead of system-wide.
This allows you to use different versions and packages for each project, and to launch other projects without having to consider the impact of dependencies.
Using venv is recommended for creating virtual environments starting with version 3.5. (As of 2023/08)
actually make it
Now let's try it out.
If you have version 3.3 or later, it 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 named test_dir and create a virtual environment inside it.
The command for creating a venv (python -m venv) is the same on Linux.
Create it with the name myenv1 to facilitate the management of the virtual environment.
mkdir test_dir cd test_dir python -m venv myenv1
Some columns are hidden for ease of viewing, but the contents of the venv are as follows.
This time we will focus on how to use it, so we will omit the details, but these directories/files are important in configuring a virtual environment, so please do not change them 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
Enabling a virtual environment
Enable the virtual environment with the following command.
After execution, if the environment name is displayed at the beginning of the prompt, it has been successfully enabled.
.\myenv1\Scripts\Activate.ps1 (myenv1) PS C:\Users\ichik\test_dir>
*The command varies depending on the execution environment, so please refer to the table below.
platform | shell | Command to enable virtual environment |
---|---|---|
POSIX | bash/zsh | $ source /bin/activate |
fish | $ source /bin/activate.fish |
|
csh/tcsh | $ source /bin/activate.csh |
|
PowerShell | $ /bin/Activate.ps1 |
|
Windows | cmd.exe | C:\> \Scripts\activate.bat |
PowerShell | PS C:\> \Scripts\Activate.ps1 |
While the environment name is displayed, the virtual environment Python will be executed instead of the base Python.
Note that when creating a standard venv, the Python version of the virtual environment and the base Python version are the same.
python -V Python 3.11.0
As mentioned earlier, the packages on the virtual environment are independent, so you can use them by simply installing them as you like.
Disabling a virtual environment
Deactivate the virtual environment with the following command.
(myenv1) PS C:\Users\ichik\test_dir> deactivate
If the virtual environment itself is no longer needed, there is no problem in deleting the entire directory.
ex ) I want to copy and reuse the virtual environment
For example, if you want to use it on a different PC or share it with others for development, you will not copy and reuse the venv itself.
Create a list of packages and share the source code with git etc.
You can output a list of packages with the following command.
(myenv1) PS C:\Users\ichik\test_dir> python -m pip freeze > requirements.txt
Now, 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 launch a virtual environment with a different version by installing any version.
First, check the installed version.
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 one using Python 3.9.
py -3.9 -m venv pyenv39 .\pyenv39\Scripts\Activate.ps1
A virtual environment has been launched with a different version from the base!
(pyenv39) PS C:\Users\ichik\test_dir> python -V Python 3.9.13
After that, you can run the program with a different version by referring to the source code and requirements.txt here and performing pip install.
summary
- venv is a module for creating lightweight Python virtual environments on top of base Python added in version 3.3
- Different versions and packages can be used for different projects
- It is possible to launch other projects without having to consider the impact of dependencies.
Thank you for viewing.