【问题标题】:Packaging python project with multiple directories用多个目录打包python项目
【发布时间】:2020-02-12 05:33:33
【问题描述】:

我需要一些关于使用 setuptoolsfind_packages 函数的解释。 我有一个这样的项目结构:

├── project_dir_1
│   ├── module.py
│   ├── __init__.py
├── my_project
│   ├── cli.py
│   ├── subdir1
│   │   ├── __init__.py
│   │   ├── module.py
│   ├── conf
│   │   ├── module.py
│   │   ├── params
│   │   │   ├── config.yml
│   │   ├── __init__.py
│   ├── subdir2
│   │   ├── module.py
│   ├── __init__.py
│   └── version.py
├── project_dir_2
│   ├──  subdir1
│   │   ├── module.py
│   │   ├── __init__.py
│   ├── __init__.py
├── README.md
├── requirements.txt
├── setup.py
└── tests
    └── test_main.py

实际上我在my_project 目录中的所有代码,我还有两个附加目录project_dir_1project_dir_2,其中包含我应该在包代码和另一个项目代码中导入的必要外部模块软件包将安装在 venv 中。 我有这样的设置脚本:

setup(
    name='my_project',
    version='0.0.1',
    description='Python library.',
    license='license',
    author='me',
    author_email='my_email',
    entry_points={'console_scripts': ['my_project=my_project.cli:main']},
    python_requires='>=3.7',
    packages=find_packages(
        include=['my_project', 'project_dir_1', 'project_dir_2', 'my_project.*', 'project_dir_1.*', 'project_dir_2.*']
    ),
    install_requires=list(open(join(dirname(__file__), 'requirements.txt')).read().split()),
)

当我在另一个项目文件夹中激活 venv 并尝试从包根文件夹(如python ..\package_root\setup.py install)安装包时,安装过程中一切似乎都正常。 pip list 显示所有依赖项和 my_project 0.0.1。但是,如果我尝试使用 venv 解释器从 my_project 导入某些内容,则会出现错误:ModuleNotFoundError: No module named 'my_project'。如果我尝试导入类似from project_dir_1 import module 的东西,结果相同,这也是必要的。另外,当我从 shell 寻址到 cli 运行 my_project 时,我得到了一个错误:

Traceback (most recent call last):
  File "/home/developer/another_project/env/bin/my_project", line 11, in <module>
    load_entry_point('my_project==0.0.1', 'console_scripts', 'my_project')()
  File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/home/developer/another_project/env/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'my_project'

那么组织这个复杂的项目结构并在setup.py 中包含所有必要代码以正确安装包的正确方法是什么?setuptools?我需要对 python 项目打包有一些更好的了解,但仍然无法从寻求文档中得到这个案例的答案。

【问题讨论】:

  • 我相信您应该始终运行python setup.py install,这意味着当前工作目录应该始终是包含setup.py 文件的目录。我真的建议不要从另一个目录运行setup.py。另一方面,我相信你可以做到pip install ../package_root,虽然不完全确定。
  • 你找到解决方案了吗?

标签: python setuptools packaging setup.py project-structure


【解决方案1】:

find_packages 将解析相对于当前工作目录的路径,因此在项目根目录之外调用它不会有效地安装任何东西(检查您是否看到通过例如运行安装的任何源

$ pip show -f my_project

,我敢打赌不会列出任何内容)。您必须在设置脚本中强制切换到项目根目录,例如在您的设置脚本中添加一条神奇的线:

# setup.py

import os
from setuptools import setup

# old-style for python 2
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

# new style for python 3
from pathlib import Path
os.chdir(Path(__file__).parent.absolute())

setup(...)

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 2018-06-02
    • 2021-07-07
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多