Third-party libraries
Python ships with a lovely standard library, which is a collection of packages and modules that are available on every machine that runs Python. However, you'll soon find that it doesn't contain everything you need. When this happens, you have two options:
- Write a supporting package yourself
- Use somebody else's code
We won't be covering the details about turning your packages into libraries, but if you have a problem you need to solve and you don't feel like coding it (the best programmers are extremely lazy and prefer to reuse existing, proven code, rather than write their own), you can probably find the library you want on the Python Package Index (PyPI) at http://pypi.python.org/. Once you've identified a package that you want to install, you can use a tool called pip
to install it.
You can install packages using an operating system command such as the following:
% python -m pip install mypy
If you try this without making any preparation, you'll either be installing the third-party library directly into your system Python directory, or, more likely, will get an error that you don't have permission to update the system Python.
The common consensus in the Python community is that you don't touch any Python that's part of the OS. Older Mac OS X releases had a Python 2.7 installed. This was not really available for end users. It's best to think of it as part of the OS; and ignore it and always install a fresh, new Python.
Python ships with a tool called venv
, a utility that gives you a Python installation called a virtual environment in your working directory. When you activate this environment, commands related to Python will work with your virtual environment's Python instead of the system Python. So, when you run pip
or python
, it won't touch the system Python at all. Here's how to use it:
cd project_directory
python -m venv env
source env/bin/activate # on Linux or macOS
env/Scripts/activate.bat # on Windows
(For other OSes, see https://docs.python.org/3/library/venv.html, which has all the variations required to activate the environment.)
Once the virtual environment is activated, you are assured that python -m pip
will install new packages into the virtual environment, leaving any OS Python alone. You can now use the python -m pip install mypy
command to add the mypy tool to your current virtual environment.
On a home computer – where you have access to the privileged files – you can sometimes get away with installing and working with a single, centralized system-wide Python. In an enterprise computing environment, where system-wide directories require special privileges, a virtual environment is required. Because the virtual environment approach always works, and the centralized system-level approach doesn't always work, it's generally a best practice to create and use virtual environments.
It's typical to create a different virtual environment for each Python project. You can store your virtual environments anywhere, but a good practice is to keep them in the same directory as the rest of the project files. When working with version control tools like Git, the .gitignore
file can make sure your virtual environments are not checked into the Git repository.
When starting something new, we often create the directory, and then cd
into that directory. Then, we'll run the python -m venv env
utility to create a virtual environment, usually with a simple name like env
, and sometimes with a more complex name like CaseStudy39
.
Finally, we can use one of the last two lines in the preceding code (depending on the operating system, as indicated in the comments) to activate the environment.
Each time we do some work on a project, we can cd
to the directory and execute the source
(or activate.bat
) line to use that particular virtual environment. When switching projects, a deactivate
command unwinds the environment setup.
Virtual environments are essential for keeping your third-party dependencies separate from Python's standard library. It is common to have different projects that depend on different versions of a particular library (for example, an older website might run on Django 1.8, while newer versions run on Django 2.1). Keeping each project in separate virtual environments makes it easy to work in either version of Django. Furthermore, it prevents conflicts between system-installed packages and pip
-installed packages if you try to install the same package using different tools. Finally, it bypasses any OS permission restrictions surrounding the OS Python.
There are several third-party tools for managing virtual environments effectively. Some of these include virtualenv
, pyenv
, virtualenvwrapper
, and conda
. If you're working in a data science environment, you'll probably need to use conda
so you can install more complex packages. There are a number of features leading to a lot of different approaches to solving the problem of managing the huge Python ecosystem of third-party packages.