Install dependencies from setup.py

It is interesting, for example, like .deb packages, this 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 explored the internet, but everything I found out just left me confused, things like β€œrequires”, β€œinstall_requires” and β€œrequirements.txt”

+11
source share
2 answers

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

 gunicorn docutils>=0.3 lxml==0.5a7 

Then create a setup.py script and read 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, [...]) 

Performing the installation python setup.py install install your package and all the dependencies. As @jwodder said that it is not necessary to create a requirements.txt file, you can simply install install_requires directly in the setup.py script. But writing the requirements.txt file is good practice.

In the configuration function, you must also install version , packages , author , etc. Read the document for a complete example: https://docs.python.org/3.7/distutils/setupscript.html

Your dir package will look like this:

 β”œβ”€β”€ yourpackage β”‚  β”œβ”€β”€ yourpackage β”‚  β”‚  β”œβ”€β”€ __init__.py β”‚  β”‚  └── yourmodule.py β”‚  β”œβ”€β”€ requirements.txt β”‚  └── setup.py 
+1
source

Another possible solution

 try: # for pip >= 10 from pip._internal.req import parse_requirements except ImportError: # for pip <= 9.0.3 from pip.req import parse_requirements def 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")) 
0
source

Source: https://habr.com/ru/post/1206762/


All Articles