简体   繁体   English

打包多个目录的python项目

[英]Packaging python project with multiple directories

I need some explanation on working with setuptools and find_packages function. I have a project structure like this:我需要一些关于使用setuptoolsfind_packages function 的解释。我有一个这样的项目结构:

├── 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

Actually all my code in the my_project dir and I also have two additonal dirs project_dir_1 and project_dir_2 that contains necessary external modules from where I should do imports both in the package code and in another projects code where this package will be installed in venv.实际上,我的所有代码都在my_project目录中,我还有两个附加目录project_dir_1project_dir_2 ,它们包含我应该从中导入的必要外部模块 package 代码和另一个项目代码,其中 package 将安装在 venv 中。 I have setup script like this:我有这样的设置脚本:

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()),
)

When I activate venv in another project folder and trying to install package from package root folder like python..\package_root\setup.py install everything seems to works fine during install.当我在另一个项目文件夹中激活 venv 并尝试从 package 根文件夹(如python..\package_root\setup.py install )安装 package 时,安装过程中一切似乎都正常。 And pip list shows all dependencies and my_project 0.0.1 . pip list显示所有依赖项和my_project 0.0.1 But if I'm trying to import something from my_project using the venv interpreter I got an error: ModuleNotFoundError: No module named 'my_project' .但是,如果我尝试使用 venv 解释器从 my_project 导入某些内容,我会收到一个错误: ModuleNotFoundError: No module named 'my_project' The same result if I'm trying to import something like from project_dir_1 import module which is also necessary.如果我尝试导入类似from project_dir_1 import module的内容,结果相同,这也是必需的。 Also when I just run my_project from shell addressing to cli I got an error:另外,当我从 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'

So what is the right way to organize this complex project structure and include all necessary code in setup.py to get the setuptools install the package correctly?那么组织这个复杂项目结构并在setup.py中包含所有必要代码以使setuptools正确安装 package 的正确方法是什么? I need some better understanding of python projects packaging but still don't get an answers for this case from seeking the docs.我需要更好地了解 python 项目打包,但仍然无法通过查找文档获得此案例的答案。

find_packages will resolve paths relative to current working directory, so calling it outside of the project root dir will effectively install nothing (check whether you see any sources installed by eg running find_packages将解析相对于当前工作目录的路径,因此在项目根目录之外调用它不会有效地安装任何东西(检查你是否看到任何安装的源,例如运行

$ pip show -f my_project

, I bet nothing will be listed). ,我打赌什么都不会列出)。 You have to force switching to the project root dir in the setup script, eg add a magic line to your setup script:您必须在安装脚本中强制切换到项目根目录,例如,在您的安装脚本中添加一条魔法线:

# 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(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM