How to include and install local dependencies in setup.py in Python?

Question:

I am creating a setup.py to distribute my application.
This application has a number of dependencies which can be installed via pip, it also has some custom dependencies which can not be installed from PyPI.

So, I have created a custom_package_0.1.whl which will be included into the distribution and must be installed as a dependency after setup.py installs everything from install_requires.

Imagine the following app structure:

my_app/
    win_deps/custom_package_0.1.whl
    my_app/
        __init__.py
        main.py
        setup.py
        setup.cfg

How do I do that?

Asked By: minerals

||

Answers:

There are several options that you can choose from:

  1. Upload your package to some server, and provide the URL with dependency_links.
  2. You could put your python package inside of your my_app package and link it with packages parameter, instead of using the wheel file.
  3. A more hacky way would be to use the setuptools api, and install the package by yourself.
Answered By: Gal Ben David

it is possible but not sure what setuptools version you should use.
steps:

in setup.py

setup(
  ...,
  install_requires=['my-package'],
  dependency_links=[
    # location to your egg file
    os.path.join(os.getcwd(), 'deps', 'my_package-1.0.0-py3.5.egg')
  ]
)

important thing is that your location should not pass URL pattern test and egg file name should have structure <package_name_with_no_hyphens>-<version>-<py_version>.egg

Answered By: wiesiu_p

Extending wiesiu_p’s answer, you can install the dependency by linking to its source directory, which has its own setup.py.

Assume you have the source files of your dependency my-dependency, and the root of my-dependency has its own setup.py. In your application’s setup.py:

setup(
  ...,
  install_requires=['other-dependency','my-dependency'],
  dependency_links=[
    # location to your my-dependency project directory
    ''.join(['file:\', os.path.join(os.getcwd(), 'path', 'to', 'my-dependency#egg=my-dependency-1.0')])
  ]
)

Now if you run python setup.py install with your application’s setup.py, it will install my-dependency.

Answered By: Anfernee

There is a new technique (Since version 19.1) called Direct references.
Just pretend like your file is hosted on localhost.

from setuptools import setup

path_to_my_project = "/home/user/projects/my_package"  # Do any sort of fancy resolving of the path here if you need to


setup(# ... other arguments
      install_requires=[f"my_package @ file://localhost/{path_to_my_project}#egg=my_package"]
      )
Answered By: RunOrVeith

Based on @RunOrVeith answer above this works for a local wheel file using a relative path. So it can be used on various hosts to install a third party package. Works on Windows and Unix.

setup(# ... other arguments
      install_requires=[
          f"my-lib @ file://localhost/{os.getcwd()}/libs/my_lib-xxx-none-any.whl"
      ]
)
Answered By: HeyMan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.