This article comes from : Getting started <http://www.pythondoc.com/pythontutorial3/appetite.html>
Appetizer reference : appetizer <https://blog.csdn.net/lvsehaiyang1993/article/details/81054512>
use Python interpreter : use Python interpreter
<https://blog.csdn.net/lvsehaiyang1993/article/details/81060747>
In this paper, we discuss the relationship between Python Introduction to :Python brief introduction
<https://blog.csdn.net/lvsehaiyang1993/article/details/81113004>
Python Process introduction : thorough Python technological process
<https://blog.csdn.net/lvsehaiyang1993/article/details/81134064>
Python data structure :Python data structure
<https://blog.csdn.net/lvsehaiyang1993/article/details/81141514>
Python: modular :Python modular
<https://blog.csdn.net/lvsehaiyang1993/article/details/81152869>
Python: Input and output Python Input and output
<https://blog.csdn.net/lvsehaiyang1993/article/details/81209254>
Python: Errors and exceptions Python Errors and exceptions
<https://blog.csdn.net/lvsehaiyang1993/article/details/81217932>
Python: class Python: class
<https://blog.csdn.net/lvsehaiyang1993/article/details/81253371>
Python: Standard library Preview Python: Standard library Preview
<https://blog.csdn.net/lvsehaiyang1993/article/details/81282695>
Python: Standard library Preview 2Python: Standard library Preview 2
<https://blog.csdn.net/lvsehaiyang1993/article/details/81432915>
#12. Virtual environments and packages
##12.1. brief introduction

Python Applications often use packages and modules that are not part of the standard library . Applications sometimes require a specific version of the library , Because it needs a specific bug
The library that has been fixed or it was written using the interface of an obsolete version of the library .

This means that you may not be able to install one Python To meet the requirements of each application . If the application A Requires a specific module 1.0 Version but application B The 2.0
edition , The two applications are in conflict , Installation version 1.0 Or version 2.0 One of the applications will not run .

The solution to this problem is to create a virtual environment ( Commonly referred to as “virtualenv”), Containing a specific version of Python, And a separate directory tree for some additional packages .

Different applications can use different virtual environments . To resolve the conflict in the previous example , application program A You can have your own virtual environment , Where specific modules are installed 1.0 edition . And applications B
Have another one with a specific module installed 2.0 Version of virtual environment . If the application B Need a library upgrade to 3.0 The words of , It also doesn't affect the application A The environment of .
12.2. Create virtual environment

The scripts used to create and manage virtual environments are called pyvenv.pyvenv Usually you will install the Python The latest version in . This script can also specify that a specific version of the
Python, So if you have multiple versions of Python The words of , You can run it pyvenv-3.5 Or any version you want to select a specific one Python edition .

To create a virtualenv, First decide on a directory you want to store, and then run it pyvenv It's followed by the directory name :
pyvenv tutorial-env
If the directory doesn't exist , This will create a tutorial-env catalog , Also create a directory containing Python interpreter , Standard library , And various supporting documents
Python “ copy ”.

Once you have created a virtual environment , You have to activate it .

stay Windows upper , function :
tutorial-env/Scripts/activate
stay Unix perhaps MacOS upper , function :
source tutorial-env/bin/activate
( This script uses bash shell Written . If you use csh perhaps fish shell, You should use it activate.csh and
activate.fish To replace .)

Activating the virtual environment will change you shell Prompt , Displays the virtual environment you are using , And changed the environment to run python Will give you a specific Python edition . for example :
-> source ~/envs/tutorial-env/bin/activate (tutorial-env) -> python Python
3.5.2+ (3.4:c7b9645a6f35+, May 22 2015, 09:31:25) ... >>> import sys >>>
sys.path ['', '/usr/local/lib/python35.zip', ...
'~/envs/tutorial-env/lib/python3.5/site-packages'] >>>
##12.3. use pip Management Pack

Once you activate a virtual environment , You can use a pip Program to install , Upgrade and remove packages . By default pip Will be from Python Package Index,
https://pypi.python.org/pypi <https://pypi.python.org/pypi>, Installation package . You can go through web
The browser browses them , Or you can use it pip Limited search capabilities :
(tutorial-env) -> pip search astronomy skyfield - Elegant astronomy for Python
gary - Galactic astronomy and gravitational dynamics. novas - The United States
Naval Observatory NOVAS astronomy library astroobs - Provides astronomy
ephemeris to plan telescope observations PyAstronomy - A collection of
astronomy related tools for Python.
pip There are many subcommands :“ search ”,“ install ”,“ uninstall ”,“freeze”( translator's note : There is no suitable word to translate this word for the time being ), wait .( Please refer to
installing-index Guide acquisition pip More complete documentation .)

You can install the latest version of a package , By specifying the name of the package :
-> pip install novas Collecting novas Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas Running setup.py install for novas
Successfully installed novas-3.1.1.3
You can also install a specific version of the package , By giving the package name followed by == And version number :
-> pip install requests==2.6.0 Collecting requests==2.6.0 Using cached
requests-2.6.0-py2.py3-none-any.whl Installing collected packages: requests
Successfully installed requests-2.6.0
If you rerun the command (pip install requests==2.6.0),pip
You will notice that the required version is already installed , Not going to do anything . You can also provide a different version number to install , Or run pip install --upgrade To upgrade the package to the latest version :
-> pip install --upgrade requests Collecting requests Installing collected
packages: requests Found existing installation: requests 2.6.0 Uninstalling
requests-2.6.0: Successfully uninstalled requests-2.6.0 Successfully installed
requests-2.7.0
pip uninstall Following one or more package names will remove the packages from the virtual environment .

pip show Information about a specified package will be displayed :
(tutorial-env) -> pip show requests --- Metadata-Version: 2.0 Name: requests
Version: 2.7.0 Summary: Python HTTP for Humans. Home-page:
http://python-requests.org Author: Kenneth Reitz Author-email:
[email protected] License: Apache 2.0 Location:
/Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages Requires:
pip list All packages installed in the virtual environment are listed :
(tutorial-env) -> pip list novas (3.1.1.3) numpy (1.9.2) pip (7.0.3) requests
(2.7.0) setuptools (16.0)
pip freeze A list similar to the packages that need to be installed will be generated , But the output uses pip install Expected format . It's common to put them in one
requirements.txt file :
(tutorial-env) -> pip freeze > requirements.txt (tutorial-env) -> cat
requirements.txt novas==3.1.1.3 numpy==1.9.2 requests==2.7.0
requirements.txt Can be submitted to version control and as part of an application . Users can use install -r Install all required packages :
-> pip install -r requirements.txt Collecting novas==3.1.1.3 (from -r
requirements.txt (line 1)) ... Collecting numpy==1.9.2 (from -r
requirements.txt (line 2)) ... Collecting requests==2.7.0 (from -r
requirements.txt (line 3)) ... Installing collected packages: novas, numpy,
requests Running setup.py install for novas Successfully installed
novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
pip There are more options . Please refer to installing-index
<https://docs.python.org/3/installing/index.html#installing-index> Guide to getting about pip
Complete documentation . When you write a package and create a Python Package Index If it appears in the , Please refer to distributing-index
<https://docs.python.org/3/distributing/index.html#distributing-index> guide .