• No results found

Managing Packages with pip

In document and the Python development team (Page 102-107)

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>. You can browse the Python Package Index by going to it in your web browser, or you can usepip’s limited search feature:

(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.

...

piphas a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. (Consult the installing-index guide for complete documentation forpip.)

You can install the latest version of a package by specifying a package’s name:

(tutorial-env) $ 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 a package by giving the package name followed by==and the version number:

(tutorial-env) $ 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 re-run this command,pipwill 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 runpip install --upgrade to upgrade the package to the latest version:

Python Tutorial, Release 3.6.4

(tutorial-env) $ 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 uninstallfollowed by one or more package names will remove the packages from the virtual environ-ment.

pip showwill display information about a particular package:

(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 listwill display all of the packages installed in the virtual environment:

(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 freezewill produce a similar list of the installed packages, but the output uses the format that pip installexpects. A common convention is to put this list in arequirements.txtfile:

(tutorial-env) $ pip freeze > requirements.txt (tutorial-env) $ cat requirements.txt

novas==3.1.1.3 numpy==1.9.2 requests==2.7.0

The requirements.txtcan then be committed to version control and shipped as part of an application.

Users can then install all the necessary packages withinstall -r:

(tutorial-env) $ 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

12.3. Managing Packages with pip 97

piphas many more options. Consult the installing-index guide for complete documentation forpip. When you’ve written a package and want to make it available on the Python Package Index, consult the distributing-index guide.

CHAPTER

THIRTEEN

WHAT NOW?

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?

This tutorial is part of Python’s documentation set. Some other documents in the set are:

• library-index:

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.

• installing-index explains how to install additional modules written by other Python users.

• 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.

More Python resources:

• 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.

• https://docs.python.org: Fast access to Python’s documentation.

• 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.

• 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.)

• http://www.pyvideo.orgcollects links to Python-related videos from conferences and user-group meet-ings.

• 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.

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]. 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 athttps://mail.python.org/pipermail/.

99

Before posting, be sure to check the list of Frequently Asked Questions (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.

CHAPTER

FOURTEEN

INTERACTIVE INPUT EDITING AND HISTORY SUBSTITUTION

Some versions of the Python interpreter support editing of the current input line and history substitution, similar to facilities found in the Korn shell and the GNU Bash shell. This is implemented using the GNU Readlinelibrary, which supports various styles of editing. This library has its own documentation which we won’t duplicate here.

14.1 Tab Completion and History Editing

Completion of variable and module names is automatically enabled at interpreter startup so that theTab key invokes the completion function; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such asstring.a, it will evaluate the expression up to the final '.'and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a __getattr__()method is part of the expression. The default configuration also saves your history into a file named.python_historyin your user directory. The history will be available again during the next interactive interpreter session.

In document and the Python development team (Page 102-107)