构建引用 setup.py 中父目录中的包的包

Ale*_*vey 3 python setuptools setup.py

我正在尝试从 Git 存储库中的源代码构建 pip 包,该存储库具有多个共享公共包的包。(我不允许更改此 Git 存储库的结构。)

\n

结构是:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 run_helpers\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 aws.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 s3.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 components\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 redshift_unload\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 redshift_unload\n        \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 run.py\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 setup.py\n
Run Code Online (Sandbox Code Playgroud)\n

我的setup.py如下:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 run_helpers\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 aws.py\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 s3.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 components\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 redshift_unload\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 redshift_unload\n        \xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n        \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 run.py\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 setup.py\n
Run Code Online (Sandbox Code Playgroud)\n

看看这里的其他答案,到目前为止我尝试过的事情包括:

\n
    \n
  • 在行中指定实际的包名称,packages=而不是使用find_packages().
  • \n
  • 传递where="../../"find_packages()
  • \n
  • 使用find_packages() + find_packages(where="../../") in the packages=` 行。
  • \n
  • 我能想到的都在packages_dir一线。
  • \n
\n

当我运行时pip install .,包安装得很好,但是当我运行已安装的 python 脚本时,我得到:

\n
from setuptools import setup, find_packages\n\nsetup(\n    ...\n    packages=find_packages(),\n    package_dir={"": "."},\n    entry_points={\n        "console_scripts": ["redshift_unload=redshift_unload.run:main"]\n    }\n)\n
Run Code Online (Sandbox Code Playgroud)\n

做了什么工作:

\n
    \n
  • 如果我将common目录移动到components/redshift_unload,那么它就可以正常工作。但我不能这样做。我还尝试在其位置放置一个符号链接,但似乎也不起作用。
  • \n
\n

有办法让这项工作发挥作用吗?

\n

Ale*_*vey 5

我相信我已经找到了最好的解决方案。

根据这里的评论,我得出的结论是,我想做的事情并非有意或不受支持。

但是,我发现以下解决方法效果很好:

from pathlib import Path
from shutil import rmtree, copytree

from setuptools import setup, find_packages

src_path = Path(os.environ["PWD"], "../../common")
dst_path = Path("./common")

copytree(src_path, dst_path)

setup(
    ...
    packages=find_packages(),
    package_dir={"": "."},
    entry_points={
        "console_scripts": ["redshift_unload=redshift_unload.run:main"]
    }
)

rmtree(dst_path)
Run Code Online (Sandbox Code Playgroud)

这里的关键见解是,当打包发生在临时目录中时, 的值os.environ["PWD"]可供进程使用,这样可以临时复制公共目录,然后再次清理(使用shutil函数copytreermtree)到将找到的位置在调用find_packages()该函数之前。setup()