Virtualenv and howtovirtualenv in the HowTo Series
1. Introduction
Virtualenv is a command line tool used to create an independent Python runtime environment.
When we use python for development, we will rely on different Python versions and different library versions. When our project depends on another development kit, We will download the package to the site-packages directory of python. If different projects depend on different package versions, this will inevitably lead to version conflicts in the development process.
Therefore, the purpose of the virtualenv tool is to solve a problem: isolate the Python runtime environment so that different runtime environments do not affect each other.
2. Installation
Using the python pip tool, you can directly install the virtualenv tool:
1 pip install virtualenv
3. Use
Next, we will introduce how to use virtualenv to create an independent python environment.
The simplest way to use virtualenv is simple. You can simply use a single line of command:
1 virtualenv isolate2 3 #Using base prefix #'/Library/Frameworks/Python.framework/Versions/3.4'4 #New python executable in isolate/bin/python3.45 #Also creating executable in isolate/bin/python6 #Installing setuptools, pip, wheel...done.
TheIsolateIs the root directory of the environment you want to create. After executing this command, several files and directories will be created under the isolate directory:
1 ├── bin2 ├── include3 └── lib
- The lib directory contains the library file of the newly created Python runtime environment. All the dependent packages installed in this environment will appear inLib/pythonX. X/site-packages/Directory.
- The bin directory contains some executable tools (such as pip, setuptools, and activate scripts for activating the current environment) and python interpreters for the current environment. All python scripts started using the python interpreter in the current environment use the dependency packages in the current environment, which serves to isolate the environment.
3.1 activate the environment
After the isolate environment is created, the environment is not activated. If we executePythonThe global python environment is used. We need to execute the following command in the root directory of the current environment (that is, under the isolate directory above)
1 source bin/activate
This command updates the PATH variable in our shell environment to activate the environment. After executing this command, our shell prompt will also change:
1 # Before executing the command 2 [21:21:02] ~ /Personal/isolate-> source bin/activate3 #4 (isolate) after the command is executed [21:33:29] ~ /Personal/isolate->
We can see that there are more(Isolate).
Now, if we execute the python command in shell, we use the python environment in isolate. before exiting the environment, all our operations will only affect the isolate environment, it does not affect the global environment, including installing various package versions, and only installs them in the isolate environment, which is isolated from other environments.
3.2 Exit the environment
To exit the activated environment and use the global python environment, run the following command:
1 # before execution 2 (isolate) [21:33:31] ~ /Personal/isolate-> deactivate 3 #4 after execution [21:42:14] ~ /Personal/isolate->
To uninstall the entire environment, you can directly Delete the directory where the environment is located.