In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of Python virtual environment. I hope you will get something after reading this article. Let's discuss it together.
1. What is a virtual environment?
The meaning of virtual environment, just like virtual machine, can realize that Python dependency packages in different environments are independent and do not interfere with each other. To a certain extent, this gives our project a very strong guarantee. Here, I jokingly call it "insurance". The only one in the network.
Give me an example.
Suppose we have two projects in our computer, and they all use the same third-party package, and everything is going well. However, for some reason, Project B has to use some of the new features of this third-party package (the new version is available only), and if it is upgraded hastily, we cannot assess the impact on Project A, so we especially need a solution that allows projects An and B to be in two different Python environments. It doesn't affect each other.
In order to facilitate your understanding of the virtual environment, I list its advantages:
Make different application development environments independent
Environment upgrade does not affect other applications, nor does it affect the global python environment
It can prevent package management confusion and version conflicts in the system.
There are many tools on the market to manage Python versions and environments. Here are a few:
P: a very simple interactive python version management tool.
Pyenv: a simple Python version management tool.
Vex: commands can be executed in a virtual environment.
Virtualenv: a tool for creating a standalone Python environment.
A set of extensions to virtualenvwrapper:virtualenv.
There are many tools, but I think it is best to use virtualenvwrapper, which is recommended to be used by everyone.
2.virtualenv
Because virtualenvwrapper is a set of extensions to virtualenv, if you want to use virtualenvwrapper, you must first install virtualenv.
Installation
Pip install virtualenv# check version virtualenv-- version
Because virtualenv created the virtual environment in the current environment. So we need to prepare a directory dedicated to the virtual environment. (the following operation is completed in Linux, windows is relatively simple, please complete it by yourself, if you don't understand, please contact me by qq. )
Create
Prepare the directory and do $mkdir-p / home/wangbm/Envs$ cd! $# create a virtual environment (according to the default Python version) # after execution, there will be a my_env01 directory $virtualenv my_env01# under the current directory. You can also specify the version $virtualenv-p / usr/bin/python2.7 my_env01 $virtualenv-p / usr/bin/python3.6 my_env02#. You must think it's troublesome to specify a version every time. # under Linux, you can write this option into the environment variable $echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7" > > ~ / .bashrc
Enter / exit
$cd / home/wangbm/Envs# enter $source my_env01/bin/activate# and exit $deactivate
To delete the virtual environment, just delete the corresponding folder. Does not affect the global Python and other environments.
$cd / home/wangbm/Envs$ rm-rf my_env01
Note: the virtual environment created will not contain the third-party package of the native global environment, which will ensure the cleanliness of the new virtual environment.
If you need to use the same third party package as the global environment. You can use the following methods:
# Export dependency package $pip freeze > requirements.txt# install dependency package $pip install-r requirements.txt
# # 3. Virtualenvwrapper
Although virtualenv has been quite easy to use, the function is still not perfect.
You may also find that in order to enter a virtual environment, you have to keep in mind the virtual environment directory you set up before. it's okay if you install the environment in a fixed directory every time you follow the rules. But in many cases, people will be lazy, and there may be many virtual environments scattered all over the system, and you may forget their names or locations.
Also, it takes two steps for virtualenv to switch the environment, exit-> enter. It's not easy enough.
In order to solve these two problems, virtualenvwrapper was born.
Installation
# installation-Linuxpip install virtualenvwrapper# installation-Windowspip install virtualenvwrapper-win
Configure find the location of the virtualenvwrapper.sh file first
Find /-name virtualenvwrapper.sh# / usr/bin/virtualenvwrapper.sh
Added in ~ / .bashrc file
Export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/workspaceexport VIRTUALENVWRAPPER_SCRIPT=/usr/bin/virtualenvwrapper.shsource / usr/bin/virtualenvwrapper.sh actual combat demonstration
Next, let's take a look at how to use our virtual environment in our development
The scenarios we usually use are as follows
Interactive
In PyCharm
Under construction
Next, I will show you one by one.
Interactive
Let's first compare the difference between a global environment and a virtual environment. There are requests packages in the global environment, but not installed in the virtual environment. When we type in workon my_env01, there is a my_env01 logo in front of it, indicating that we are already in a virtual environment. All subsequent operations will be performed in a virtual environment.
In the project
Our project has an entry file. If you look at it carefully, the first line can specify a Python interpreter.
If we want to run the project in a virtual environment, just change the file header.
Now I'm going to take import requests as an example to illustrate whether it is running in a virtual environment, and if so, then, as above, there will be an error.
Contents of the file:
#! / root/.virtualenvs/my_env01/bin/pythonimport requestsprint "ok"
Before running, be careful to add execution permissions.
$chmod + x ming.py
Okay. Let's execute it.
$. / ming.py
Found that, as expected, the report was really wrong. It shows that the virtual environment we specified is effective.
In PyCharm
Click File-Settings-Project-Interpreter
Click the pinion. Click add as shown in the figure and follow the prompts to add a virtual environment. Then click OK to use the virtual environment, and all subsequent projects will run in this virtual environment.
After reading this article, I believe you have some understanding of "sample Analysis of Python Virtual Environment". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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: 297
*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.