如何将本地Python模块作为根目录中的单独包安装在子目录中?

问题描述 投票:0回答:1

我有一个具有这种结构的存储库:

root_dir/
├── python/
│   └── libraries/
│       └──types/
│           ├── type1/
│           │   └── math/
│           │       └── math.py
│           └── type2/
│               └── categories/
│                   └── category1/ 
│                       ├── module1/
│                       │   └── module1.py
│                       ├── module2/
│                       │   ├──module2.py
│                       │   └── test/
│                       │       └── test_module2.py
│                       └── module3/
│                           ├── module3.py
│                           └── test/
│                               └── test_module3.py
└── notebooks/
    └── type1/
        └── categories/
            └── category1/
                └── notebook.ipynb
            

如何从根目录将

root_dir\python\libraries\types\type1
安装为
types.type1
root_dir\python\libraries\types\type2\categories
types.type2.categories
root_dir
作为两个单独的包?我希望能够使用以下命令将这些模块导入到
notebook.ipynb

from types.type1.math.math import Vector
from types.type2.categories.category1.module1.module1 import Calculator
from types.type2.categories.category1.module2.module2 import Processor
python python-import python-packaging
1个回答
0
投票

将 setup.py 文件添加到 root_dir\python\libraries 中,如下所示:

from setuptools import setup, find_namespace_packages

setup(
    name='types.type1',
    version='0.1',
    packages=find_namespace_packages(where='types', include=['type1.math']),
    package_dir={'': 'types'},
)

将第二个 setup.py 文件复制到 root_dir\python\libraries 中,如下所示:

from setuptools import setup, find_namespace_packages

setup(
    name='types.type2.categories',
    version='0.1',
    packages=find_namespace_packages(where='type2'),
    package_dir={'': 'type2'},
    install_requires=[
        'types.type1',
    ]
)

已经解决了问题。我现在可以使用这些导入语句:

from type1.math.math import Vector
from categories.category1.module1.module1 import Calculator
from categories.category1.module2.module2 import Processor

这对于我的项目来说效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.