If you are responsible for multiple Python projects at the same time, or if you want to easily package a project and its associated library files,
Or if you're concerned about possible conflicts between installed libraries, you can install a Python virtual environment to divide and
The rule.
When a Python library is installed without a virtual environment, you are actually installing it globally. This usually requires a tube
Administrator privileges, or as root, this library file is stored for each user on the device and for each project
In the. Fortunately, creating a virtual environment is simple:
$ virtualenv scrapingenv
This creates a new environment called SCRAPINGENV, which you need to activate before using:
$ CD scrapingenv/
$ source Bin/activate
After activating the environment, you will notice that the environment name appears in front of the command prompt, reminding you that you are currently in the virtual ring.
The environment. Any libraries you install later and any programs you execute are running in this environment.
In the new SCRAPINGENV environment, you can install and use BeautifulSoup:
(scrapingenv) ryan$ pip install Beautifulsoup4
(scrapingenv) ryan$ python
> from BS4 import beautifulsoup
>
When you no longer use a library in a virtual environment, you can exit the environment by releasing the command:
(scrapingenv) ryan$ deactivate
ryan$ python
> from BS4 import beautifulsoup
Traceback (most recent):
File "<stdin>", line 1, in <module>
Importerror:no module named ' BS4 '
All the libraries associated with the project are placed in a single virtual environment, and can be easily packaged for the entire environment to occur to other
People. As long as their Python version is the same as yours, your packaged code can be shipped directly through the virtual environment.
No need to install any more libraries.
Python learning----Saving a library file with a virtual environment