如何从Python中的子模块安装依赖项? [英] How to install a dependency from a submodule in Python?
问题描述
我有一个具有以下结构的Python项目(为简单起见,省略了无关的源文件):
I have a Python project with the following structure (irrelevant source files omitted for simplicity):
myproject/
mysubmodule/
setup.py
setup.py
文件 myproject/setup.py
使用 distutils.core.setup
来安装模块 myproject
及其相关源.但是, myproject
需要安装 mysubmodule
(这是一个git子模块).所以我现在正在做的是:
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.
调用 myproject/setup.py
时是否可以自动安装 mysubmodule
?
Is there a way to automate the installation of mysubmodule
when calling myproject/setup.py
?
推荐答案
使用自己的 setup.py
为 mysubmodule
创建一个程序包,并让顶层程序包依赖在该程序包的 setup.py
中.这意味着您只需要使软件包/依赖项可用,并在顶级软件包上运行 python setup.py install
即可.
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.
然后,问题就变成了如何将依赖项/软件包运送给客户,但这可以通过以下方法解决:将它们放在目录中,并配置 setup.py
在搜索依赖项时包括该目录.
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.
替代方法是供应商" mysubmodule
,这意味着将其全部包含在一个软件包中(不再询问其他问题),并安装一个 python setup.py install
主程序包.例如, pip
供应商(包括) requests
,因此它可以使用它而不必依赖于该 requests
包.
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.
这篇关于如何从Python中的子模块安装依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!