1.5. Installing packages¶
One of the strengths of the Python language, besides its huge standard library (“batteries included”), is its well-developed modular package system, allowing to install packages provided by the community for nearly every task at hand.
The central place to look for Python packages is the Python Package Index (PyPI). What is even better: Python comes with a package manager included: “pip”. This is a very powerful command-line program allowing to install packages including their dependencies and much more. For a general introduction, see the official Python documentation.
1.5.1. Don’t install packages globally¶
Just as a friendly reminder: It is never a good idea to change your system-wide Python installation, particularly so on systems (like many Linux distributions) where Python can be mission-critical. Always use a virtual environment.
1.5.2. Install a package¶
Installing a package is as simple as issuing a single command in the terminal, provided you know its name. Suppose you would like to install numpy
– a good choice if you plan to program anything science-related:
(python-course) foo@foobar:~$ pip install numpy
Of course, the actual command to type is pip install numpy
. The reason to show you once again the full terminal prompt is to remind you to check that you’ve activated a virtual environment, as in this case “python-course”. Remember that the currently active virtual environment is usually shown at the prompt – quite conveniently.
If you install a package like “numpy” in a new virtual environment, you will see that a series of other packages will be installed as well. Here you see the strength of the Python package manager: dependencies can be specified for a particular package, and they will be checked for existence upon installing the package and (only) installed as well if necessary, i.e., if a dependency is already installed, it will usually not be installed or updated.
1.5.3. Update a package¶
Sometimes you need to update a package. If it is the package manager pip itself a new version is available for, it will tell you on the command line when you install (or update) other packages, and it will tell you how to do. In all other cases, the command is simple, though not as intuitive (to me) as it could be:
pip install --upgrade <package>
Here, <package>
is the name of the package you would like to update. Note that it is safe to issue the “–upgrade” command, even if there is no newer version available. The package manager will simply tell you in this case:
Requirement already satisfied: <package> in /path/to/site-packages