foreword
For beginners, there will be some environmental problems, and the virtual environment and why it is used are not very understood. The purpose of this article is to solve these problems.
Related Links:
Python installation third-party library prompts "neither an internal or external command nor a runnable program or batch file" solution
1. How the python and pip commands work
First of all, let's talk about the working principle of the python command. Taking Windows as an example, when our Python is installed, the executable program is an .exe file (other forms of executable files in other systems).
Open python.exe directly, and a window for entering commands will open.
But if we use the command line to enter Python, we can also enable Python's command line. What is the principle?
The answer is revealed: There is a place in the system that tells the terminal "I know where this program Python is".
Now let's talk about the environment variable .
2. Environment variables
What are environment variables?
Imagine a scenario. If you are going to school for the first time, you plan to go to the cafeteria to eat at noon, but you don't know where the cafeteria is.
You can turn the whole campus by yourself, and finally find the cafeteria with your own efforts, and then go in to eat;
Of course, you can also ask the teacher directly, and the teacher will tell you "turn left at a certain intersection and walk 500 meters to arrive".
In fact, the computer also needs to find resources by " asking for directions ", so there are two situations:
The user searches by himself, finds where the resource is placed, and then lets the computer execute it according to this path;
Put a "map" in the computer, the location of the resource is marked on the map, and then the computer only needs to look at the "map" to know where a certain software is placed.
This "map" is the environment variable .
In the actual running process, if the resource directory is not specified, the computer will only in the current directory of (System32 .
What if the computer can't find it? For example, if you enter Python and the computer can't find any resources related to Python, it can only prompt "It is neither an internal or external command nor a runnable program or batch file. " up...
The essence of environment variables is a key-value pair: that is, variable name = path.
When Python is installed, you can choose to add the Python software to the environment variable , so that the system knows that Python's home lives in the directory of the C drive.
But there is a small problem with this,
What if different projects require different Python version ?
What if the third-party packages required by different projects conflict with each other ?
Thus was born a feature called "Virtual Environments".
3. Virtual environment
In general, for Python and pip commands, their paths are permanently written under the installation directory .C:/Program Files/Python3.x/Python.exe
If you want to switch versions, the most stupid way is to install multiple Pythons and then manually set the environment variables each time, but this is too inefficient.
The role of the virtual environment is: in addition to the global environment variables, additional generates a set of Python files, and in this project, the environment variable temporary points to this set of paths, instead of global path .
For example: D:/project/venv/python3.x/python.exe
(venv is the directory of Python's native virtual environment)
If there are multiple projects, the norm is to have one virtual environment per project.
In this way, various third-party packages can be installed for the virtual environment without affecting the global environment .
On the one hand, you can use different Python versions for different projects, so that each project does not interfere with each other.
On the other hand, if there are various problems with the virtual environment, you only need to delete it and create a new one, and it will not affect the environment of other projects.
4. Summary
- In Windows, the principle of terminal input Python is: Python.exe is executed (other systems are other forms of executable file ).
- The reason why the user is not required to enter the path is because the system knows where Python.exe is.
- The computer finds where Python is through the environment variable .
- In order to resolve conflicts between different versions and different packages, virtual environment .
- A virtual environment creates another set of independent environments, containing files like Python.exe and pip.exe.
- The principle for the virtual environment to take effect is to make the Python environment variable temporarily point to another Python file of instead of the global environment variable.
postscript
For students who have been exposed to the front-end, the function of the virtual environment is the same as that of NVM. NVM is also a manager used to manage the coexistence of multiple versions of Node.js.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。