How to install a dependency from a submodule in Python?

*爱你&永不变心* 提交于 2021-01-26 23:07:21

问题


I have a Python project with the following structure (irrelevant source files omitted for simplicity):

myproject/
    mysubmodule/
        setup.py
    setup.py

The file myproject/setup.py uses distutils.core.setup to install the module myproject and the relevant sources. However, myproject requires mysubmodule to be installed (this is a git submodule). So what I am doing right now is:

myproject/$ cd mysubmodule
myproject/mysubmodule/$ python setup.py install
myproject/mysubmodule/$ cd ..
myproject/$ python setup.py install

This is too tedious for customers, especially if the project will be extended by further submodules in the future.

Is there a way to automate the installation of mysubmodule when calling myproject/setup.py?


回答1:


Create a package for mysubmodule with its own setup.py and let the top-level package depend on that package in its setup.py. This means you only need to make the packages / dependencies available and run python setup.py install on the top-level package.

The question then becomes how to ship the dependencies / packages to your customers but this can be solved by putting them in a directory and configuring setup.py to include that directory when searching for dependencies.

The alternative is to "vendor" mysubmodule which simply means including it all in one package (no further questions asked) and having one python setup.py install to install the main package. For example, pip vendors (includes) requests so it can use it without having to depend on that requests package.




回答2:


setuptools.find_packages() is able to discover submodules

Your setup.py should look like

from setuptools import setup, find_packages

setup(
    packages=find_packages(),
# ...
)


来源:https://stackoverflow.com/questions/28113862/how-to-install-a-dependency-from-a-submodule-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!