11.1 Using External Libraries (pip, virtualenv) in Python
Python provides a rich ecosystem of external libraries and packages that can greatly extend the functionality of your programs. These libraries can be easily installed using pip
, Python’s package manager. To manage dependencies for different projects and avoid conflicts between them, you can use virtualenv
, which creates isolated environments for your projects.
In this section, we will explore how to install external libraries using pip
, manage project dependencies with virtualenv
, and best practices for using external packages in Python.
11.1.1 Installing External Libraries with pip
pip
is the default package manager for Python, and it allows you to install, upgrade, and manage external libraries from the Python Package Index (PyPI), which hosts thousands of Python packages.
Checking if pip
is Installed
In most cases, pip
is already installed when you install Python. You can check if pip
is installed by running the following command in your terminal or command prompt:
pip --version
This will display the version of pip
if it is installed. If pip
is not installed, you may need to install it manually or ensure that Python is properly installed on your system.
Basic pip
Commands
Here are some common pip
commands for managing external libraries:
Listing installed packages:
pip list
This will show all the packages currently installed in your environment.
Uninstalling a package:
pip uninstall package_name
This will remove the specified package from your environment.
Upgrading a package:
pip install --upgrade package_name
This will upgrade the package to the latest version available.
Installing a specific version:
pip install package_name==version_number
This will install a specific version of the package.
Installing a package:
pip install package_name
This will install the package specified by package_name
from PyPI.
Example: Installing and Using an External Library
Let’s install the popular requests
library, which is used to make HTTP requests.
pip install requests
Once the package is installed, you can import and use it in your Python script:
import requests
response = requests.get('https://api.github.com')
print(response.status_code) # Output: 200 (if successful)
print(response.json()) # Output: JSON response from GitHub API
In this example:
- The
requests.get()
method is used to make a GET request to the GitHub API, and the status code and response are printed.
11.1.2 Using virtualenv
for Isolated Environments
When working on multiple Python projects, each project may require different versions of libraries or even different versions of Python itself. To avoid conflicts between dependencies, you can use virtualenv
to create isolated environments for each project.
What is virtualenv
?
A virtualenv
(virtual environment) is a self-contained directory that contains a Python interpreter and the libraries specific to a project. This ensures that the libraries you install for one project don’t interfere with libraries in another project or your global Python installation.
Installing virtualenv
You can install virtualenv
using pip
:
pip install virtualenv
Creating a Virtual Environment
Once virtualenv
is installed, you can create a new virtual environment in your project directory:
virtualenv venv
venv
is the name of the virtual environment directory (you can name it anything you like).
This will create a directory called venv
that contains a local copy of Python and its standard library. You can then install libraries in this environment without affecting the global Python installation.
Activating the Virtual Environment
To start using the virtual environment, you need to activate it. The activation command depends on your operating system:
On macOS and Linux:
source venv/bin/activate
On Windows:
venv\Scripts\activate
Once activated, your terminal prompt will change to indicate that the virtual environment is active (e.g., (venv)
), and any packages you install using pip
will be installed in this environment.
Deactivating the Virtual Environment
To deactivate the virtual environment, simply run the following command:
deactivate
This will return you to your system’s global Python environment.
11.1.3 Managing Project Dependencies with requirements.txt
When working on a project, you may want to keep track of the external libraries and their versions so that others can easily reproduce the environment. You can use a requirements.txt
file to list the packages required by your project.
Creating a requirements.txt
File
You can generate a requirements.txt
file that lists all the installed packages in your virtual environment by running:
pip freeze > requirements.txt
This command will create a requirements.txt
file with all the currently installed packages and their versions.
Installing Packages from requirements.txt
To install all the packages listed in a requirements.txt
file, use the following command:
pip install -r requirements.txt
This will install all the dependencies specified in the file, ensuring that the environment is set up correctly for the project.
11.1.4 Using pipenv
as an Alternative to virtualenv
pipenv
is an alternative to virtualenv
that combines both package management and virtual environment creation into one tool. It simplifies dependency management by automatically creating virtual environments and locking dependencies.
Installing pipenv
You can install pipenv
using pip
:
pip install pipenv
Creating a Virtual Environment and Installing Packages with pipenv
To create a virtual environment and install a package with pipenv
, use the following command:
pipenv install package_name
This will create a virtual environment, install the package, and generate a Pipfile
and Pipfile.lock
to manage the dependencies.
Activating a pipenv
Environment
To activate the virtual environment created by pipenv
, use:
pipenv shell
This will activate the virtual environment and allow you to run your Python scripts within the environment.
Using Pipfile
for Dependency Management
Instead of a requirements.txt
file, pipenv
uses a Pipfile
to manage dependencies. The Pipfile
lists the required packages and their versions, while the Pipfile.lock
file locks the exact versions of the dependencies to ensure a reproducible environment.
11.1.5 Best Practices for Using External Libraries
- Use Virtual Environments: Always use virtual environments (via
virtualenv
orpipenv
) to avoid conflicts between dependencies and keep your global Python installation clean. - Pin Versions in
requirements.txt
: When generating arequirements.txt
file, make sure to pin the exact versions of your dependencies to avoid compatibility issues in the future. - Update Dependencies Carefully: Before upgrading a package, test the new version in your project to ensure it doesn’t introduce breaking changes. You can use
pip install --upgrade package_name
to upgrade specific packages. - Use
pip freeze
to Track Dependencies: After installing new libraries, update yourrequirements.txt
file withpip freeze > requirements.txt
to ensure that all dependencies are listed correctly. - Document Your Dependencies: Always include a
requirements.txt
(orPipfile
if usingpipenv
) in your project repository, so others can easily set up the required environment.
11.1.6 Summary
pip
: Python’s package manager used to install, upgrade, and manage external libraries.virtualenv
: A tool to create isolated Python environments for managing dependencies across different projects.pipenv
: An alternative tool that combines package management and virtual environment creation.requirements.txt
: A file used to list the dependencies required by a project, which can be generated usingpip freeze
.- Best Practices: Always use virtual environments, pin dependency versions, and keep track of project dependencies with a
requirements.txt
orPipfile
.
By using pip
and virtualenv
(or pipenv
), you can manage external libraries and dependencies efficiently, ensuring your projects are portable, reproducible, and free from conflicts between packages.