Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install dependencies from setup.py

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"

like image 640
adinanp Avatar asked Nov 13 '14 02:11

adinanp


People also ask

How do I install Python packages from setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

How do you install dependencies in Python?

In this case, you have two options: Use the pipdeptree utility to gather a list of all dependencies, create a requirements. txt file listing all the dependencies, and then download them with the pip download command. Get the list of dependencies for a package from the setup.py file.

Does pip install use setup py?

For installing packages in “editable” mode (pip install --editable), pip will invoke setup.py develop , which will use setuptools' mechanisms to perform an editable/development installation.

What is install requires in setup py?

install_requires is a setuptools setup.py keyword that should be used to specify what a project minimally needs to run correctly. When the project is installed by pip, this is the specification that is used to install its dependencies.


2 Answers

Just create requirements.txt in your lib folder and add 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 = [] # 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 
like image 147
hayj Avatar answered Sep 25 '22 02:09

hayj


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"))  
like image 20
Logovsky Dmitry Avatar answered Sep 26 '22 02:09

Logovsky Dmitry