Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26bea6c

Browse files
khanhnnvngitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent 9ee0f7e commit 26bea6c

3 files changed

Lines changed: 188 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# 12. Virtual Environments and Packages
2+
3+
### 12.1. Introduction
4+
5+
Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.
6+
7+
This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.
8+
9+
The solution for this problem is to create a [virtual environment](https://docs.python.org/3/glossary.html#term-virtual-environment), a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
10+
11+
Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.
12+
13+
### 12.2. Creating Virtual Environments
14+
15+
The module used to create and manage virtual environments is called [`venv`](https://docs.python.org/3/library/venv.html#module-venv). [`venv`](https://docs.python.org/3/library/venv.html#module-venv) will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running `python3` or whichever version you want.
16+
17+
To create a virtual environment, decide upon a directory where you want to place it, and run the [`venv`](https://docs.python.org/3/library/venv.html#module-venv) module as a script with the directory path:
18+
19+
```text
20+
python3 -m venv tutorial-env
21+
```
22+
23+
This will create the `tutorial-env` directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
24+
25+
Once you’ve created a virtual environment, you may activate it.
26+
27+
On Windows, run:
28+
29+
```text
30+
tutorial-env\Scripts\activate.bat
31+
```
32+
33+
On Unix or MacOS, run:
34+
35+
```text
36+
source tutorial-env/bin/activate
37+
```
38+
39+
\(This script is written for the bash shell. If you use the **csh** or **fish** shells, there are alternate `activate.csh` and `activate.fish` scripts you should use instead.\)
40+
41+
Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running `python` will get you that particular version and installation of Python. For example:
42+
43+
```text
44+
$ source ~/envs/tutorial-env/bin/activate
45+
(tutorial-env) $ python
46+
Python 3.5.1 (default, May 6 2016, 10:59:36)
47+
...
48+
>>> import sys
49+
>>> sys.path
50+
['', '/usr/local/lib/python35.zip', ...,
51+
'~/envs/tutorial-env/lib/python3.5/site-packages']
52+
>>>
53+
```
54+
55+
### 12.3. Managing Packages with pip
56+
57+
You can install, upgrade, and remove packages using a program called **pip**. By default `pip` will install packages from the Python Package Index, <[https://pypi.python.org/pypi](https://pypi.python.org/pypi)>. You can browse the Python Package Index by going to it in your web browser, or you can use `pip`’s limited search feature:
58+
59+
```text
60+
(tutorial-env) $ pip search astronomy
61+
skyfield - Elegant astronomy for Python
62+
gary - Galactic astronomy and gravitational dynamics.
63+
novas - The United States Naval Observatory NOVAS astronomy library
64+
astroobs - Provides astronomy ephemeris to plan telescope observations
65+
PyAstronomy - A collection of astronomy related tools for Python.
66+
...
67+
```
68+
69+
`pip` has a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. \(Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`.\)
70+
71+
You can install the latest version of a package by specifying a package’s name:
72+
73+
```text
74+
(tutorial-env) $ pip install novas
75+
Collecting novas
76+
Downloading novas-3.1.1.3.tar.gz (136kB)
77+
Installing collected packages: novas
78+
Running setup.py install for novas
79+
Successfully installed novas-3.1.1.3
80+
```
81+
82+
You can also install a specific version of a package by giving the package name followed by `==` and the version number:
83+
84+
```text
85+
(tutorial-env) $ pip install requests==2.6.0
86+
Collecting requests==2.6.0
87+
Using cached requests-2.6.0-py2.py3-none-any.whl
88+
Installing collected packages: requests
89+
Successfully installed requests-2.6.0
90+
```
91+
92+
If you re-run this command, `pip` will notice that the requested version is already installed and do nothing. You can supply a different version number to get that version, or you can run `pip install --upgrade` to upgrade the package to the latest version:
93+
94+
```text
95+
(tutorial-env) $ pip install --upgrade requests
96+
Collecting requests
97+
Installing collected packages: requests
98+
Found existing installation: requests 2.6.0
99+
Uninstalling requests-2.6.0:
100+
Successfully uninstalled requests-2.6.0
101+
Successfully installed requests-2.7.0
102+
```
103+
104+
`pip uninstall` followed by one or more package names will remove the packages from the virtual environment.
105+
106+
`pip show` will display information about a particular package:
107+
108+
```text
109+
(tutorial-env) $ pip show requests
110+
---
111+
Metadata-Version: 2.0
112+
Name: requests
113+
Version: 2.7.0
114+
Summary: Python HTTP for Humans.
115+
Home-page: http://python-requests.org
116+
Author: Kenneth Reitz
117+
Author-email: [email protected]
118+
License: Apache 2.0
119+
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
120+
Requires:
121+
```
122+
123+
`pip list` will display all of the packages installed in the virtual environment:
124+
125+
```text
126+
(tutorial-env) $ pip list
127+
novas (3.1.1.3)
128+
numpy (1.9.2)
129+
pip (7.0.3)
130+
requests (2.7.0)
131+
setuptools (16.0)
132+
```
133+
134+
`pip freeze` will produce a similar list of the installed packages, but the output uses the format that `pip install`expects. A common convention is to put this list in a `requirements.txt` file:
135+
136+
```text
137+
(tutorial-env) $ pip freeze > requirements.txt
138+
(tutorial-env) $ cat requirements.txt
139+
novas==3.1.1.3
140+
numpy==1.9.2
141+
requests==2.7.0
142+
```
143+
144+
The `requirements.txt` can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with `install -r`:
145+
146+
```text
147+
(tutorial-env) $ pip install -r requirements.txt
148+
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
149+
...
150+
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
151+
...
152+
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
153+
...
154+
Installing collected packages: novas, numpy, requests
155+
Running setup.py install for novas
156+
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
157+
```
158+
159+
`pip` has many more options. Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`. When you’ve written a package and want to make it available on the Python Package Index, consult the [Distributing Python Modules](https://docs.python.org/3/distributing/index.html#distributing-index) guide.
160+

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
* [9. Classes](9.-classes.md)
1414
* [10. Brief Tour of the Standard Library](10.-brief-tour-of-the-standard-library.md)
1515
* [11. Brief Tour of the Standard Library — Part II](11.-brief-tour-of-the-standard-library-part-ii.md)
16+
* [12. Virtual Environments and Packages](12.-virtual-environments-and-packages.md)
17+
* [13. What Now?](untitled.md)
1618

untitled.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 13. What Now?
2+
3+
Reading this tutorial has probably reinforced your interest in using Python — you should be eager to apply Python to solving your real-world problems. Where should you go to learn more?
4+
5+
This tutorial is part of Python’s documentation set. Some other documents in the set are:
6+
7+
* [The Python Standard Library](https://docs.python.org/3/library/index.html#library-index):
8+
9+
You should browse through this manual, which gives complete \(though terse\) reference material about types, functions, and the modules in the standard library. The standard Python distribution includes a _lot_ of additional code. There are modules to read Unix mailboxes, retrieve documents via HTTP, generate random numbers, parse command-line options, write CGI programs, compress data, and many other tasks. Skimming through the Library Reference will give you an idea of what’s available.
10+
11+
* [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) explains how to install additional modules written by other Python users.
12+
* [The Python Language Reference](https://docs.python.org/3/reference/index.html#reference-index): A detailed explanation of Python’s syntax and semantics. It’s heavy reading, but is useful as a complete guide to the language itself.
13+
14+
More Python resources:
15+
16+
* [https://www.python.org](https://www.python.org/): The major Python Web site. It contains code, documentation, and pointers to Python-related pages around the Web. This Web site is mirrored in various places around the world, such as Europe, Japan, and Australia; a mirror may be faster than the main site, depending on your geographical location.
17+
* [https://docs.python.org](https://docs.python.org/): Fast access to Python’s documentation.
18+
* [https://pypi.python.org/pypi](https://pypi.python.org/pypi): The Python Package Index, previously also nicknamed the Cheese Shop, is an index of user-created Python modules that are available for download. Once you begin releasing code, you can register it here so that others can find it.
19+
* [https://code.activestate.com/recipes/langs/python/](https://code.activestate.com/recipes/langs/python/): The Python Cookbook is a sizable collection of code examples, larger modules, and useful scripts. Particularly notable contributions are collected in a book also titled Python Cookbook \(O’Reilly & Associates, ISBN 0-596-00797-3.\)
20+
* [http://www.pyvideo.org](http://www.pyvideo.org/) collects links to Python-related videos from conferences and user-group meetings.
21+
* [https://scipy.org](https://scipy.org/): The Scientific Python project includes modules for fast array computations and manipulations plus a host of packages for such things as linear algebra, Fourier transforms, non-linear solvers, random number distributions, statistical analysis and the like.
22+
23+
For Python-related questions and problem reports, you can post to the newsgroup _comp.lang.python_, or send them to the mailing list at [[email protected]](mailto:python-list%40python.org). The newsgroup and mailing list are gatewayed, so messages posted to one will automatically be forwarded to the other. There are hundreds of postings a day, asking \(and answering\) questions, suggesting new features, and announcing new modules. Mailing list archives are available at [https://mail.python.org/pipermail/](https://mail.python.org/pipermail/).
24+
25+
Before posting, be sure to check the list of [Frequently Asked Questions](https://docs.python.org/3/faq/index.html#faq-index) \(also called the FAQ\). The FAQ answers many of the questions that come up again and again, and may already contain the solution for your problem.
26+

0 commit comments

Comments
 (0)