Install dependencies from setup.py

adinanp picture adinanp · Nov 13, 2014 · Viewed 23.4k times · Source

I wonder if as well as .deb packages for example, it is possible in my setup.py I configure the dependencies for my package, and run:

$ sudo python setup.py install

They are installed automatically. Already researched the internet but all I found out just leaving me confused, things like "requires", "install_requires" and "requirements.txt"

Answer

hayj picture hayj · Oct 30, 2018

Just create requirements.txt in your lib folder and write all dependencies like this:

gunicorn
docutils>=0.3
lxml==0.5a7

Then create a setup.py script and read the requirements.txt in:

import os
thelibFolder = os.path.dirname(os.path.realpath(__file__))
requirementPath = thelibFolder + '/requirements.txt'
install_requires = [] # Examples: ["gunicorn", "docutils>=0.3", "lxml==0.5a7"]
if os.path.isfile(requirementPath):
    with open(requirementPath) as f:
        install_requires = f.read().splitlines()
setup(name="yourpackage", install_requires=install_requires, [...])

The execution of python setup.py install will install your package and all dependencies. Like @jwodder said it is not mandatory to create a requirements.txt file, you can just set install_requires directly in the setup.py script. But writing a requirements.txt file is a good practice.

In the setup function you also have to set version, packages, author, etc, read the doc for a complete example: https://docs.python.org/3/distutils/setupscript.html

You package dir will look like this:

├── yourpackage
│   ├── yourpackage
│   │   ├── __init__.py
│   │   └── yourmodule.py
│   ├── requirements.txt
│   └── setup.py