Simple Python Dependency Management
While powerful tools like virtualenv, pipenv, conda, and Poetry exist to provide extra advantages for dependency management, I’ve found a simple system that works for the small projects I’ve been working on.
This setup involves built-in features and tools that come bundled with Python by default.
When I start a new project, I create a new directory for all of its files.
mkdir my_python_project
Then, I change into the directory.
cd my_python_project
Next, I create a virtual environment.
python -m venv venv
This creates a new directory called venv
. You can call it whatever you want, but I stick with this for all my projects for consistency. The new directory contains all the files and scripts necessary to manage a virtual environment.
Each session, before starting any work, I activate the virtual environment
# macOS and Linux
source venv/bin/activate
# Windows
.\venv\Scripts\activate
You’ll know you did it correctly when your prompt changes to have (venv)
at the beginning. Remember, you must execute the above command each time you start a new coding session.
Installing packages works as usual with pip install
.
pip install scikit-learn
But each time I use pip install
, I freeze the dependencies into requirements.txt
immediately after.
pip freeze > requirements.txt
This generates a requirements.txt
file which keeps track all the required dependencies.
Any time I’m pulling in code on a new project, whether it’s my own code or someone else’s, I know when I see requirements.txt
, that I can install the required packages with pip
.
pip install -r requirements.txt
Managing dependencies this way is simple but powerful and it works great for the kinds of projects I work on.
Subscribe to my newsletter
Read articles from Travis Horn directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Travis Horn
Travis Horn
I have a passion for discovering and working with cutting-edge technology. I am a constant and quick learner. I enjoy collaborating with others to solve problems. I believe helping people achieve their goals helps me achieve mine.