Python
is favored by everyone because of its rich extension libraries. However, as the number of third-party packages in the development environment increases, it becomes more and more difficult to port and migrate applications developed based on this environment. Just like in an operating system, we can use containers (such as docker
) to separate the development environment from the production environment. When developing Python applications, we also need to provide a separate runtime environment for each application to avoid unnecessary version conflicts and troubleshooting costs.
That's where virtualenv comes in.
Installation#
Installation is actually very simple.
First, you must have pip
installed (who doesn't install it when installing Python?)
Then enter the command
pip3 install virtualenv
Wait for the installation to complete and you're done!
Note: Python 3 already includes the venv module, so you don't need to install it manually. Check if it is installed by running the command
python3 -m venv --help
Basic Operations#
Creating a Virtual Environment#
To create a virtual environment at the desired location, simply enter
virtualenv test
After a while, a virtual environment named "test" will be created. In the future, all third-party packages downloaded in this virtual environment will be placed in this folder.
The following image indicates a successful creation:
All the configurations of your virtual environment are stored in the test folder.
The default directory structure is as follows (Windows 10 environment):
- test
- Include
- Lib
- Script
- tcl
- LICENCE.txt
Virtual environments created with older versions of the command will inherit third-party packages that exist in the actual environment. If you need to create a clean virtual environment,
Enter:
virtualenv --no-site-packages test
The latest version creates a clean environment without third-party packages by default.
Entering the Virtual Environment#
For Windows
systems, go to the Script
folder and execute the activate.bat
file.
For Linux
, execute:
source test/bin/activate
test is the folder where your virtual environment is located.
When the virtual environment name appears at the beginning of the command line, it means that you have switched to the virtual environment. From now on, the third-party dependencies you install will be downloaded to this virtual environment, without affecting the actual Python runtime environment on your machine. In fact, these dependencies are downloaded to the folder where this virtual environment is located.
Exiting the Virtual Environment#
For Windows
, enter
For Linux
systems, execute
source test/bin/deactive
Handing Over the Environment#
This practice is quite common when you need to transfer your development environment to another computer or hand it over to someone else. You need to copy your virtual environment to another location.
The steps are as follows:
- Freeze the environment packages and save the package version information in a text file.
$ pip freeze > requirements.txt
- Download all dependencies in another environment directly.
$ pip install -r requirements.txt
Other Commands#
Usage:
$ virtualenv [OPTIONS] DEST_DIR
Options:
–version
#Display the current version number.
-h, –help
#Display help information.
-v, –verbose
#Display detailed information.
-q, –quiet
#Do not display detailed information.
-p PYTHON_EXE, –python=PYTHON_EXE
#Specify the version of the python interpreter used, for example, –python=python2.5 uses the 2.5 version of the interpreter to create a new isolated environment. By default, the python interpreter installed on the current system (/usr/bin/python) is used.
–clear
#Clear the installation for non-root users and start creating the isolated environment from scratch.
–no-site-packages
#Prevent the isolated environment from accessing the system's global site-packages directory.
–system-site-packages
#Allow the isolated environment to access the system's global site-packages directory.
–unzip-setuptools
#Unzip Setuptools or Distribute during installation.
–relocatable
#Relocate an existing isolated environment. Using this option will fix the script and make all .pth files use relative paths.
–distribute
#Use Distribute instead of Setuptools. The same effect can be achieved by setting the environment variable VIRTUALENV_DISTRIBUTE.
–extra-search-dir=SEARCH_DIRS
#The directory used to search for setuptools/distribute/pip release packages. You can add any number of –extra-search-dir paths.
–never-download
#Prohibit downloading any data from the Internet. In this case, if the local search for release packages fails, virtualenv will report an error.
–prompt==PROMPT
#Define the command line prefix of the isolated environment.
Components#
Let's take a look at the directory structure of the virtual environment (Windows 10 environment):
- [Virtual Environment Name]
- Include
- Lib
- Script
- tcl
- LICENCE.txt
- Include:
- Lib: Library files
- Script: Script files
- tcl
First, let's introduce the Python runtime environment.
Having Fun with VScode#
How to deploy a virtual environment in
VScode
Method 1#
Add the following line to the \.vscode\launch.json
file of the project:
"pythonPath": "D:\\myproject\\venv\\Scripts\\python.exe",
Method 2#
Personally tested and effective
To use Python
's virtualenv
virtual environment in VScode
, first add the directory where the virtual environment is located and the recognition rules for the virtual environment to the settings:
{
...
"python.venvPath": "E:\\envs",
"python.venvFolders": [
"envs",
".pyenv",
".direnv',
".env"
]
}
Afterwards, when you want to switch the virtual environment, simply use the command palette Python: Select Interpreter
and all virtual environments will be listed.
In fact, it creates a
./.vscode/settings.json
configuration file in your project root directory and writes the interpreter path option in it. This file is called the workspace settings and can be used to set configuration options for each project individually.
Method 3#
After activating the virtual environment in the command line, simply open the project folder with code
.
XXX>venv\scripts\activate
(venv) XXX>code project_name/