【问题标题】:Sharing local python scripts between packages在包之间共享本地 python 脚本
【发布时间】:2020-03-04 14:19:36
【问题描述】:

我已经查看了这里的信息:How can I make setuptools (or distribute) install a package from the local file system

看起来这个问题是很久以前发布的,我希望在发布后的七年里,在 Python 世界中管理依赖项方面有了一些新的发展。

具体来说,在我们的案例中,我们正在开发相关 GCP 包的存储库:

src/
    airflow/
        __init__.py
        dags/
            __init__.py
            requirements.txt
            dag1.py
            libs/
                __init__.py
                utils.py
        tests/
             dags/
                 test_dag1.py
        plugins/
    dataflow/
        __init__.py
        setup.py
        dataflow1.py
        libs/
             __init__.py
             utils.py
    cli_helper/
         __init__.py
         cli_command.py
         libs/
             __init__.py
             util.py
    shared_utils/
        util1.py

我发现自己在每个包的上下文中重复相同的辅助函数位,并希望将这些辅助函数放在一个位置,然后在每个包下的 shared_utils 文件夹中拥有 shared_utils 文件的链接副本,或者甚至只是将util1.py 的副本放在每个包的现有libs 目录下。

什么是最“pythonic”的方式来完成这个?

现在看来我唯一的选择是:

  1. 尽可能使用上面列出的 requirements.txt,并在 setup.py 中使用自定义命令,但不能使用 requirements.txt。
  2. 从 shared_utils 目录到每个包中创建一个操作系统级链接,以使该目录看起来本机存在于我的每个包中。
  3. 打包我的 shared_utils 然后直接从 git 安装。虽然这个选项再次需要 requirements.txt 并且在我的一些部署环境中,我不能依赖 requirements.txt,我必须通过 setup.py 运行所有内容

【问题讨论】:

    标签: python python-3.x google-cloud-platform setuptools


    【解决方案1】:

    如果您不能使用 requirements.txt,也许您可​​以使用 setuptool 的 install_requires 并转到 shared_utils 包根目录。这能解决你的问题吗?

    【讨论】: