Installing Python Packages and Modules
Ever found a brilliant Python tool that solves your problem perfectly but you had no idea how to install it? That is where installing Python modules comes into play. Whether you are just starting out or managing serious projects, knowing how to set up packages and tools is a core part of using Python effectively. In this guide, we will walk you through the basics, from virtual environments to pip, helping you get the most out of Python’s powerful package system.
Key Terms
Let us begin by understanding the tools and terms you will come across:
-
pip: This is Python’s go-to installer. From version 3.4 onwards, pip comes bundled with Python.
-
Virtual environment: Think of it as a separate workspace for your project so packages do not interfere with others. venv is Python’s built-in tool for this since version 3.3.
-
virtualenv: An earlier tool similar to venv, mainly for older Python versions before 3.4.
-
Python Package Index: This is an online collection of packages you can install using pip.
-
Python Packaging Authority: This group maintains packaging tools and documentation. You can find their work on GitHub.
-
distutils: An older build tool that laid the foundation for current packaging systems. Although it is being phased out, you may still encounter it.
Basic Usage
You will use the command line to install packages. Here is how to get started:
python -m pip install SomePackage
To install a specific version, use:
python -m pip install SomePackage==1.0.4
For a minimum version, use quotes to avoid shell issues:
python -m pip install "SomePackage>=1.0.4"
If the module is already installed, running the command again does nothing unless you explicitly ask for an upgrade:
python -m pip install --upgrade SomePackage
Want more on pip’s capabilities? Check out the Python Packaging User Guide.
For working in virtual environments, use the venv module. Create the environment, activate it, then install packages as shown above.
How Do I …?
Here are quick answers to common questions.
… install pip for Python versions before 3.4?
Earlier versions do not bundle pip, so you will need to set it up manually. Follow the guidance in the requirements section of the Python Packaging User Guide.
… install packages for just myself?
Use the –user option:
python -m pip install --user SomePackage
On Windows, use the py launcher:
py -3.4 -m pip install SomePackage
Common Installation Issues
Installing Python into the System on Linux
Linux distributions often include Python. Installing packages into this system version requires root access and may conflict with your system tools. To avoid trouble, use a virtual environment or install packages just for your user.
Pip Not Installed
If pip was not installed with Python, you can add it with:
python -m ensurepip --default-pip
Additional help for installing pip is available in the packaging guide.
Installing Binary Extensions
Python used to rely on source-based installs, meaning users had to compile extensions manually. Now, many packages offer pre-built “wheel” files, making installs quicker and simpler. Wheels especially help Windows and macOS users. For scientific software and other compiled packages, this trend is making things more accessible.
For more on binary extensions, see the Binary Extensions Guide.
