Oregon State University
2025-01-06
These include general numerical and scientific tools like NumPy, SciPy, and Pandas, and domain-specific software like Cantera. You should rely on mature packages that provide useful features when possible—don’t reinvent the wheel!
But, each software package has its own set of dependencies, and might require a specific version of some other package (like when a newer version breaks or changes some functionality).
This is particularly important for research analysis.
pip install x
Virtual environments are lightweight installations of packages in a particular location (like in your working directory or project project) that build on top of your system Python installation.
Virtual environments isolate the set of dependencies for a project or package from each other, and from your system configuration.
These are intended to be easy to set up, disposable, and replaceable.
On a Mac or Linux machine, I recommend using the built-in venv
module to create a virtual environment for your work, then install inside that using the pip
installer.
On Windows, your best bet is probably the Anaconda Distribution. Anaconda comes with hundreds of scientific packages already, and also lets you set up system-wide virtual environments.
This creates a new directory .venv
with:
.venv/bin
with link to Python, pip
, and activation scripts; and.venv/lib/site-packages
where packages will be installed.To activate the environment, source the activation script:
Tip
Try this yourself. .venv/bin
will be added to your PATH, and your shell will usually indicate you are “in” a virtual environment.
To leave the virtual environment, run the deactivate function:
Note
Closing/terminating your shell will automatically deactivate the virtual environment, and you’ll need to manually activate whenever you start a new session.
Tools that all work similarly to venv
:
venv
: comes with Python, simplevirtualenv
: installable package with same interface as venv
, but faster and with more optionsuv
: written in Rust and extremely fastIn addition, there are project management tools that both handle environments, packaging, production, etc.:
These work a bit differently, but can be quite powerful particularly for locking specific versions of dependencies. Consider returning to these when we are thinking about packaging and distribution.
Conda is an open-source tool for managing virtual environments across your system, originally designed for Python package management but now supports a wide range of software (e.g., compiled C++ dependencies).
Virtual environments created with Conda are not location-dependent like venv
, but the same approach applies: do not install in the base/default environment.
Conda is recommended if you expect to need non-Python dependencies.
Some alternatives to conda exist and offer some advantages, including speed:
Schreiner, H., Niemeyer, K., Holland, J. G., Dang, G., & Restrepo, M. I. (2024). “INTERSECT: Packaging” [Data set]. https://github.com/INTERSECT-training/packaging