Installing Python Packages
Python's strength lies in its ecosystem of third-party packages. The pip package manager is the standard tool for installing, upgrading, and removing Python packages from the Python Package Index (PyPI).
Always ensure your virtual environment is activated before installing packages. If you do not see the (.venv) prefix in your terminal prompt, activate your environment first. See Virtual Environments for instructions.
Installing Packages with pip
Basic Installation
To install a package:
pip install <package-name>
For example, to install NumPy:
pip install numpy
pip will download the package from PyPI and install it into your active virtual environment's site-packages directory.
Installing a Specific Version
To install a specific version of a package:
pip install <package-name>==<version>
For example:
pip install pandas==2.1.4
This is useful when a project requires a specific version for compatibility.
Installing Multiple Packages
You can install multiple packages in a single command:
pip install numpy pandas matplotlib
Common pip Commands
| Command | Purpose |
|---|---|
pip install <package> | Install a package |
pip install <package>==<version> | Install a specific version |
pip install --upgrade <package> | Upgrade a package to the latest version |
pip uninstall <package> | Remove a package |
pip list | List all installed packages and their versions |
pip show <package> | Show detailed information about an installed package |
pip freeze | Output installed packages in requirements.txt format |
If pip is not recognized as a command, use python -m pip instead. This runs pip through the Python interpreter and works regardless of PATH configuration. For example: python -m pip install numpy.
Managing Dependencies with requirements.txt
A requirements.txt file lists all the packages your project depends on, along with their versions. This file is essential for team collaboration — it allows anyone to recreate your exact environment.
Creating a requirements.txt
After installing all the packages your project needs, capture them:
pip freeze > requirements.txt
This generates a file like:
matplotlib==3.8.2
numpy==1.26.3
pandas==2.1.4
scipy==1.12.0
Installing from requirements.txt
When you clone a project or set up a new environment, install all dependencies at once:
pip install -r requirements.txt
This reads the file and installs every listed package at the specified version.
Best Practices for requirements.txt
- Commit
requirements.txtto version control — This is the primary way other developers know what packages your project needs. - Update it when you add or remove packages — Run
pip freeze > requirements.txtafter making changes to your environment. - Pin versions — The
pip freezeoutput includes exact version numbers (e.g.,numpy==1.26.3), which ensures reproducible environments. Do not remove the version specifiers. - Review before committing —
pip freezecaptures everything in the environment. If you installed packages for experimentation that are not part of the project, remove them before generating the file.
Commonly Used Packages for RMC Work
The following packages are commonly used across RMC Python projects. Install only what your specific project needs.
| Package | Purpose |
|---|---|
numpy | Numerical computing — arrays, linear algebra, random numbers |
pandas | Data manipulation — DataFrames, CSV/Excel I/O, data cleaning |
matplotlib | Static plotting and visualization |
plotly | Interactive plotting and visualization |
scipy | Scientific computing — optimization, interpolation, statistics |
sympy | Symbolic mathematics — algebra, calculus, equation solving |
requests | HTTP requests — calling APIs, downloading data |
notebook | Jupyter notebooks — interactive development (see Jupyter Notebooks) |
pytest | Testing framework — write and run unit tests for your code |
openpyxl | Reading and writing Excel files (used by pandas for .xlsx support) |