【问题标题】:How can do we include all the dependencies of sub module while packaging in python?在 python 中打包时如何包含子模块的所有依赖项?
【发布时间】:2020-10-23 00:00:03
【问题描述】:

基本上我有以下项目:

my_pkg
 __init__.py
 \module1
    __init__.py
    scrip1.py
    script2.py
    requirement.txt
 \module2
    _script1.py
    _script2.py
    requirement.txt
 setup.py
 LICENSE
 README.md

我正在尝试将 requirements.txt 文件包含在我的库 my_pkg

的打包中

module1 文件夹中的 requirements.txt 包含: request

module2 文件夹中的 requirements.txt 包含: pdfkit

这是我的 setup.py 文件:

import setuptools
from setuptools import setup

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="im_pkg",
    version="0.0.1",
    author="John Doe",
    description="Shared Python library",
    long_description=long_description,
    license="MIT",
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=["requests","pdfkit"],
    python_requires='>=3.6'
)

我想动态读取我的子文件夹的 requirements.txt,而不必手动将其放入 install_requires= 部分

【问题讨论】:

  • 答案是将requirements.txt 文件扔掉。对于图书馆来说,它们毫无用处。 setup.py 中的 install_requires 字段是项目依赖项的真实来源,您已经了解了这一点。
  • 公平地说,确实存在打包工具可以捆绑requirements.txts 并基本上为您构建setup.pysetup.cfg,最流行的是pbr。但是他们通常只期望一个单一的需求文件,正如我上面所说的,他们的方法有点误导。另请参阅 python 打包工具链的主要作者之一的this post 以获取更多信息。

标签: python setuptools python-wheel python-packaging


【解决方案1】:

您应该只列出主要依赖项及其版本,以免损坏。不要列出用于开发的东西,例如yapfblackpytestflake8 等。尽可能降低要求。

但是,如果您不想这样做,this 就是您正在寻找的答案。

例子:

    install_requires=[
        "graphene==2.1.8",
        "graphene-django==2.5.0",
        "graphql-core==2.2.1",
        "psycopg2==2.8.3"
    ],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-06
    • 2011-10-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2014-11-19
    • 2013-09-15
    • 2013-04-17
    相关资源
    最近更新 更多