In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of the principle of virtual environment in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Understanding the virtual environment
In our usual work environment, there may be multiple versions of python on one computer. For example, we have a version of Python2.7 and a Python3.8 environment, both of which exist in our current system. This creates a problem that both versions are in the same environment, resulting in conflicts between pip and third-party packages.
In order to solve this problem, we specially create a Python2.7 virtual environment and a Python3.8 virtual environment. Python2.7 's environment is only Python2.7 's pip and interpreter, while Python3.8 can create its own environment.
These two environments exist independently, do not conflict with each other and do not affect each other, which is the function and purpose of the virtual environment. We just need to install the software version we need in our respective virtual environments. These software and specified versions only need to be aimed at the current virtual environment and will not affect other virtual environments.
Virtual environment tools in Python
What software is available in python to create virtual environments:
Virtualenv: a very commonly used virtual environment tool.
1. Depends on the version of Python that is available in the current system.
2. Easy to use, so it is sought after by most Python enthusiasts.
Pyenv: like Virtualenv, it is a tool for creating virtual environments.
1. It does not depend on the Python version of the current system. As long as it is the version owned by Python, pyenv can create a virtual environment on the current computer (first install the required version locally, and then virtualize it.)
2. Disadvantages: the operation is too complex, and it also depends on Virtualenv. We can understand that pyenv ultimately relies on Virtualenv to create a virtual environment.
Because of the relative complexity of pyenv operations, this chapter focuses on the use of Virtualenv, and it will be easier to focus on pyenv after you become more proficient in using python in the future.
Virtualenv
Virtualenv is also a third-party package for Python and is used on command-line terminals like ipython.
1. Download and install through pip install virtualenv.
2. When the installation is complete and we want to create the virtual environment in that directory, we need to enter the corresponding directory folder in advance, which is also the directory where we chose to create the virtual environment.
3. After entering the selected folder, execute virtualenv-p python3 penv (- p is the version of the selected python. If you want to specify the version number, you can use-p python3.6;penv as the name of the specified virtual environment).
4. When the virtualenv-p python3 penv command is executed in the selected folder, a penv folder is created in the current directory, which is the running virtual environment directory.
5. Next, call. / penv/bin/active to start the virtual environment; if it is a windows environment, you can directly enter the / penv/bin directory and directly execute active to start the virtual environment.
6. If you want to exit the virtual environment, you can exit the current virtual environment by directly executing deactivate.
It is important to note that the virtual environment we are implementing is only valid on our current Terminal terminals. If we have multiple Terminal terminals open and each of them needs to be in a virtual environment, we need to do the above on each Terminal terminal.
Treminal terminal demo
Download and install: pip install virtualenv
After the installation is completed, we use the virtualenv-p python3.10 python3.10_env command to create a virtual environment named python3.10_env. At the same time, we will find that the folder of the corresponding virtual environment has been created under the current path, as shown below:
Next we go to the python3.10_env path we created and execute the. / python3.10_env/bin/activate command to enter our virtual environment. I have two problems here.
The first problem: no executable permission, error zsh: permission denied:. / python3.10_env/bin/activate, this is because the user does not have permission, so this error occurs, so you only need to modify the permission with chmod.
The second problem: when executing. / python3.10_env/bin/activate, prompt You must source this script: $source. / python3.10_env/bin/activate; it means that I must use the command source. / python3.10_env/bin/activate to execute the virtual environment of python3.10_env. (this happens because I'm using Mac. I don't know if this will happen on WIN computers.)
Execute source. / python3.10_env/bin/activate to try to execute the ipython that we have installed and find that it cannot be executed because we are a virtual environment. Although the physical environment has ipython installed, because our virtual environment is completely isolated from the physical environment, python3.10_env is a clean environment and there is no ipython.
Take a look at the python interpreter of the current virtual environment, as shown below:
If you need to exit the virtual environment, execute deactivate. It should be noted that after exiting the virtual environment, the current environment of the terminal is not the environment before we entered the virtual environment, but the local environment, so if you want to maintain the previous state of the environment, you just need to close the Terminal terminal and re-enter.
In normal work, multiple projects may have to be developed on a computer, but multiple projects may use the same package, but different versions do. So if we are in a public environment, there is no way to guarantee that multiple versions of a package can be installed, so we can use different versions of different packages according to each different project through the virtual environment, which does not conflict with each other and ensures the independence and integrity of individual projects.
This is the end of the article on "sample Analysis of the principle of Virtual Environment in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.