Skip to content

requirements management

Managing requirements.txt can be painful when you have packages previously compiled and then you have packages that contains latest release versions, without having to constantly update or tracking differing package releases in your requirements.txt file.

Some useful python modules I've learned to use to manage these sort of package idiosyncracies are pipreqs and pip-tools.

check installed packages

# verify what packages are installed in your current venv

pip3 freeze
# verify your python version
# you can also symlink python using python3 binary
# this allows you to interchangeably use "python" or "python3"
# pip/pip3 will get also get updated automatically
python3 --version

set up requirements.txt

Always use your requirements file to add/update/delete packages as needed to start managing your environment package installs. I find it a useful way to control what packages go in and out. It's also easier for others to see what packages are needed to set up whatever your project you're trying to demo without it failing to work, even if your code runs successfully.

It's very common to have missing packages when you start sharing your project with other people. Just ensure your requirements file are updated with appropriate versions.

I personally don't recommend this approach. Why?

  1. pip freeze will installed all necessary and unnecessary dependencies and packages into the requirements file.
  2. These packages can have their own dependencies - resulting in a convoluted dependency tree as shown here.
  3. And sometimes, this is bad as some packages will sneaked up into dev code unnecessarily causing your application to break.
  4. Also this makes it difficult to manage the primary packages that you'll only need for your new project, if it's not already in your virtualenv.

Ideally, you'll want to keep your requirements.txt as cleaned as possible to control all of your environment(s) with respect to each unique project.

dependencytree

# to generate the requirements file directly via your pip freeze packages
pip3 freeze > requirements.txt
# install necessary pip or python updates as needed
# then install your packages through requirements.txt
pip3 install -r requirements.txt

manage requirements.txt and dependencies

using poetry

Last but not least! I highly recommend using Poetry to manage all of your requirements, dependeices and virtualenvs. It's literally the simplest module to use with great docs.

If you already have an exist requirements.txt, you can easily port it over using Python's pipx - essentially a Python's subprocess app to isolate dependencies install.

  1. You have to initialize your poetry directory. This will allow poetry to create a basic pyproject.toml file in the current directory.
# be sure to initialize poetry in your project directory    
poetry init
  1. Install subprocess app via pipx:
pipx poetry-add-requirements.txt
  1. then run the subprocess app,
# this will automatically detect your requirements.txt within your current directory, 
# automatically install the all your modules
poeareq