zaadeh
zaadeh

Reputation: 1871

What is the Debian equivalent of "requirements.txt" in Python for managing packages?

I'm shipping a Python app for deployment on Debian servers. Providing a requirements.txt file with a list of all needed Python packages is very convenient when installing the app, especially when accompanied with a Makefile to automatically install from it using pip.

But as you know some Python packages depend on Linux system packages (Debian in this case), and it would be great if I could provide a similar file with my project to install them all in one step, and define the Makefile rule to automate it.

Do dpkg or apt provide such functionality?

Upvotes: 1

Views: 3284

Answers (4)

zaadeh
zaadeh

Reputation: 1871

I ended up just listing the required packages as arguments to apt install command, since there were not many of them. I put the command is a Dockerfile and creating a Docker image is now my preferred way to automate how an app gets created and deployed.

Upvotes: 0

Tombart
Tombart

Reputation: 32436

Depends on your dependencies. If you have in requirements.txt dependencies like pycurl, then there's already a Debian package called python3-pycurl. Then simply add to your debian/control:

Package: mypackage
Architecture: all
Depends: ${misc:Depends}, ${python3:Depends}, python3-pycurl

There are many Python packages already ported to Debian packages.

However in some cases the Debian package might include only outdated version or there's some Pypi package that hasn't been ported to Debian yet. Since Debian 12, there's been changes to the recommended way of installing Python packages. On Python side there are certain recommendation for externally managed environments. Basically distributions should create

  • /usr/lib/python3/dist-packages system package managed by distribution
  • /usr/local/lib/python3/dist-packages packages installed by local administrator

Anyway you can still use dedicated venv for each project. This way possible conflicting dependencies between project won't collide.

python3 -m venv /usr/share/python3-myenv
source /usr/share/python3-myenv/bin/activate
python3 -m pip install -r requirements.txt

However in order to properly package the virtualenv approach you could use dh-virtualenv:

apt install dh-virtualenv

debian/control should include:

Build-Depends: debhelper (>= 9), python, dh-virtualenv (>= 0.8)

and in debian/rules use:

%:
      dh $@ --with python-virtualenv

this will ensure that dependencies in requirements.txt will be installed. Note, your project must include setup.py file. See documentation for more information.

Upvotes: 1

Caleb Marchent
Caleb Marchent

Reputation: 37

It is confusing to mix distribution packing (apt) with python's packaging(pip); if possible fulfill your dependencies through distribution provided packages. Then package using Debian's packaging method declaring the dependencies in the Depends: section of the debian/control file.

There is heaps of information available on the Debian Python Guide.

Upvotes: 1

loopbackbee
loopbackbee

Reputation: 23332

You can certainly dump all the packages installed by dpkg, but that's probably not what you want to do - you'll end up getting thounsands of packages unrelated to your software, and possibly breaking the system, if it's a different debian version.

My advice is getting your software to a fresh debian machine and try to pip install everything from your requirements.txt. As python package installation fails (because of missing debian packages), make a text file with a newline separated list of the needed debian packages.

Then, just cat my-deb-dependencies | xargs apt-get install on every new system.

This takes some manual work - I don't think there's a reliable way of automating it.

Upvotes: 1

Related Questions