Install dependencies from setup.py Install dependencies from setup.py python python

Install dependencies from setup.py


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

gunicorndocutils>=0.3lxml==0.5a7

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

import osthelibFolder = os.path.dirname(os.path.realpath(__file__))requirementPath = thelibFolder + '/requirements.txt'install_requires = [] # Here we'll get: ["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 best 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:

├── mypackage│   ├── mypackage│   │   ├── __init__.py│   │   └── mymodule.py│   ├── requirements.txt│   └── setup.py


Another possible solution

try:    # for pip >= 10    from pip._internal.req import parse_requirementsexcept ImportError:    # for pip <= 9.0.3    from pip.req import parse_requirementsdef load_requirements(fname):    reqs = parse_requirements(fname, session="test")    return [str(ir.req) for ir in reqs]setup(name="yourpackage", install_requires=load_requirements("requirements.txt"))