Your Python partners must have been more or less exposed to virtualenv. This article will introduce virtualenv and how to use virtualenv more scientifically and more gracefully.
Virtualenv
First virtualenv
, let's talk about what a ghost is.
In the Python
process of using the development, the project will inevitably encounter different projects rely on different versions of the library, or in the development process do not want to let the physical environment flooded with a variety of libraries, the future of disaster-dependent. At this point, we need to use different virtual environments for different projects to keep the development environment and the hosting environment clean. Here, we are going to introduce virtualenv
a great tool to help us manage different Python
environments. virtualenv
You can create multiple virtual environments in your system that are different and do not interfere with each other. In addition, it is worth mentioning that the virtualenv
use of installation dependencies in a virtual environment pip
can also bypass permission settings for some systems, because after all, there is no need to write data to the system directory ~ ~
So, virtualenv
How does it work?
Installation
First from the installation, the assumption is already installed pip
, if not installed pip
, please go out of Google (OK, I am a good person, attached to the pip
Official document link). Then you can use it pip
to install virtualenv
it directly. Of course, there may be a need to use sudo
elevated privileges, after all, to install in the host physical environment. Omitted in the following command sudo
, please add it yourself if required.
Select AllCopyput it in your notes .
install virtualenv
Okay, now you have the virtualenv
virtual Environment manager.
Use
Next, use.
For example, we want to do something strange, write a script to crawl the information of a website, let's call this project for the moment spider
. This project requires access to the network, and we intend to use a network access library written for humans requests
, but we do not want to install the package in the hosting environment. Let's get started.
Suppose we put this project in the /path/to/project/spider/
directory, and here we put the virtual environment directly under the Engineering directory. First, we create a virtual environment in this directory.
Select AllCopyput it in your notes .
virtualenv /path/to/project/spider
In this way, the virtual environment is set up. At this point, you can see that there are three directories under this directory that are created:
Among them, the bin
directory contains some of the commands available in this virtual environment, as well as the opening of the virtual environment of the script, activate
contains the include
virtual environment of the header files, including Python
the header files; lib
Of course, we are not yet in the virtual environment. Activating a virtual environment requires only one command.
Select AllCopyput it in your notes .
source /path/to/project/spider/bin/activate
At this point we can already be in the virtual environment.
Next, install the library required for the project requests
.
Select AllCopyput it in your notes .
install requests
Get!
At this point in the virtual environment there is a requests
library, the host environment will not be disturbed.
So how do I get out of the virtual environment? It's easier to quit, just one of the following commands.
Select AllCopyput it in your notes .
deactivate
This is the time to go back to the virtual environment and everything seems to have never happened. After many years, if you forget the location of the virtual environment, it really didn't happen. =
To add, if you want to delete a virtual environment, just remove the directory bin
include
and lib
three directories.
Virtualenvwrapper
Need for God's horse virtualenvwrapper
? This has to be said from the virtualenv
beginning.
At the end of the previous section, if you forget the location of the virtual environment, it doesn't really happen. Although it is a joke, but it will happen oh ~
virtualenv
One of the biggest drawbacks is that each time you open a virtual environment to go to the directory under the directory of the virtual environment bin
next source
activate
, this requires us to remember each virtual environment is located in the directory.
One possible solution is to centralize all of the virtual environment catalogs, for example ~/virtualenvs/
, and use different directories for different virtual environments to manage them. virtualenvwrapper
that's exactly what it does. It also eliminates the use of the virtual environment every time it is opened source
, making the virtual environment more usable.
Installation
Again, start with the installation.
Installation virtualenvwrapper
can also be used pip
in a way. If you need to join, sudo
please join yourself.
Select AllCopyput it in your notes .
install virtualenvwrapper
However, there may be an installation error on Mac OS X El Capitan, and the main problem is on a six
package called. So when installing, you can use the following methods.
Select AllCopyput it in your notes .
install virtualenvwrapper --ignore-installed six
Now we have an artifact that can manage a virtual environment.
Use
First, you need to virtualenvwrapper
configure it. It needs to specify an environment variable, called WORKON_HOME
, and needs to run its initialization tool virtualenvwrapper.sh
, which is in the /usr/local/bin/
directory. WORKON_HOME
is the directory where it will be used to store various virtual environment directories, which we can set as ~/.virtualenvs
.
Select AllCopyput it in your notes .
export WORKON_HOME=‘~/.virtualenvs‘source /usr/local/bin/virtualenvwrapper.sh
Since we need to perform these two operations each time, we can write them to the terminal's configuration file. For example, if used bash
, add to, or ~/.bashrc
zsh
add to in if used ~/.zshrc
. This will automatically run every time the terminal is started, which virtualenvwrapper
can be used after the terminal.
With this virtualenvwrapper
, we can easily create a virtual environment with the following commands.
Select AllCopyput it in your notes .
mkvirtualenv spider
Then we have a spider
virtual environment called. It is stored $WORKON_HOME/spider
under the directory.
The virtual environment is automatically activated after you create a new virtual environment. If we normally want to enter a virtual environment, we can use the following command.
Select AllCopyput it in your notes .
workon spider
This is why the directory that holds the virtual environment in the environment variable is called WORKON_HOME
. By the way, the workon
back can support the use tab
of automatic completion of yo.
Again, leave the virtual environment, which you can use.
Select AllCopyput it in your notes .
deactivate
In addition, deleting a virtual environment is just as easy.
Select AllCopyput it in your notes .
rmvirtualenv spider
End
Here, virtualenv
and virtualenvwrapper
the basic use of the introduction, you need to learn more usage, you can refer to the official document yo. I hope these two tools can help the small partners to improve their efficiency in the work yo ~ ~
Talk about virtualenv and Virtualenvwrapper practice