In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand the virtualenv virtual environment of Python". In the daily operation, I believe that many people have doubts about how to understand the virtualenv virtual environment of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to understand the virtualenv virtual environment of Python"! Next, please follow the editor to study!
Sometimes, for various reasons, we install many versions of the Python interpreter under the operating system. Similarly, we may need different versions of the module for a variety of reasons, such as Django1.8,Django1.11. Coupled with the confusion of the version of the pip tool Manager. This makes many people have great confusion in the environmental management of Python.
Usually each project, or even versions at different stages of the project, requires different Python interpreters and dependent libraries. In order to manage these environments clearly, safely and reliably, it is necessary to establish an "independent and isolated" Python environment for each project or application.
Virtualenv is a tool used to create a "separate" Python runtime environment for each project.
Prior to Python3.4, virtualenv needs to be installed manually independently before it can be executed. In later versions, Python comes with a venv library that can achieve the same effect as virtalenv by executing Python-m venv. More users are accustomed to using virtualenv.
The situation where Python multi-interpreter environment and multiple virtual environments created by virtualenv co-exist under the same operating system is generally shown below:
The core points are:
Under one operating system, there can be multiple "operating system level" Python interpreters
Each Python interpreter has its own pip tool, which corresponds to one by one.
Through virtualenv, any "operating system level" Python interpreter can be virtualized into a "virtual level" interpreter.
Each "virtual level" interpreter has its own independent pip tool and site-packages.
If you understand the above points, you will understand the difference in the use of virtualenv and pip.
Install virtualenv
What I installed below is version 16.0.4 of virtualenv. There are some instructions below in the later version, please pay attention to the difference.
Take the windows operating system as an example, to install virtualenv, it is recommended to directly use the pip specified under the current environment variable, that is, the pip bound to the default python version of your operating system, so as to facilitate command calls. It is usually as follows:
Pip install virtualenv
You can verify the installation in the command line interface: where virtualenv
As you can see, the virtualenv here is installed as an executable program under the default Python36 version of my system. There are actually three versions of Python under my current operating system:
You may have questions? What if I want the virtual Python2.7 version? Also install a virtualenv under 2.7s? You don't need it! Virtualenv can specify a virtual version of the Python interpreter, as long as it is installed on your local operating system!
Establish a virtual environment
If you manage all the virtual environments manually, you usually set up a directory to place all the virtual environments. Here: mkdir d:\ Python_virtualenvs, then run: virtualenv-p c:\ Python27\ Python2.exe-- no-site-packages d:\ Python_virtualenvs\ for_django
-p: specify the virtual Python version you want. The local python2.7 is selected here.
-- no-site-packages: means that the third-party libraries in the original version are not copied when establishing a virtual environment, so that a pure Python environment can be obtained.
D:\ Python_virtualenvs\ for_django: indicates that a virtual environment called for_django is created in this directory, so that you can tell at a glance who the virtual environment is built for.
Let's take a look at the situation in the file system, and we can see from the screenshot that the environment is very clean!
Important explanation
In February 2020, the latest version of virtualenv, taking 20.0.0 + as an example, has cancelled the-- no-site-packages parameter and directly uses-- no-site-packages mode by default. There is another parameter, system-site-packages, which provides access to the library of the source Python interpreter, but with caution. Therefore, it is recommended that you directly use the default mode virtualenv-p xxx destination.
Under ps:windows, execute the activate.bat command on the command line, and there seems to be no environment identifier in front of the prompt.
Using a virtual environment
From the command line, go to the script folder in the directory of the virtual environment you want to use, and run the activate command. Or call the command using an absolute path.
Notice the command prompt (for_django), which indicates that you are currently in the virtual environment of for_django. Now, you can use it as a normal Python environment. For example, run the python command, or use pip to install the third-party library pip install requests. These are limited and bound in the for_django virtual environment, and will not have any impact on the local Python version of the operating system and other virtual environments!
Exit the virtual environment
To exit the virtual environment, simply run the deactivate command in the script directory.
To delete the virtual environment, just delete the directory where the virtual environment is located, and be careful not to delete other environments. However, after you delete the virtual environment, all projects that previously used the environment will be affected. This is easy to understand, so make sure that the environment no longer needs to be deleted!
The use of linux operating system
For the linux operating system, the method of use is basically similar. It's just that the file path and script invocation in the virtual environment are different from those in the windows system. Take a closer look at the bin directory in the figure below to provide an activate script and a deactivate command to write environment variables.
Create a virtual environment: $virtualenv-p / usr/bin/python2.7-- no-site-packages my_venv
Start the virtual environment: $source my_venv/bin/activate
Exit the virtual environment: $deactivate
Delete virtual environment: $rm-r my_venv
Pycharm combined with the use of virtualenv
When developing under windows, it is not so convenient to use the command line, especially in the Pycharm development environment, it is also a burden to switch interfaces frequently. In fact, Pycharm can specify an interpreter and directly set the project's Python interpreter to the virtual environment you want to use, as shown below:
In settings:
Select Python:
Virtualenv cooperates with pip to establish a rapid development environment
With the freeze function of pip, virtualenv virtual environment can quickly record all the third-party modules in the whole environment at one time: pip freeze > d:\ requirements.txt
Correspondingly, it can also be used to install the third-party libraries listed in the file at one time on another machine or in a virtual environment. Just use the command: pip install-r requirements.txt
At this point, the study on "how to understand the virtualenv virtual environment of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.