在Python中,命名空间包允许您在多个项目中传播Python代码.当您想要将相关库作为单独的下载发布时,这非常有用.例如,使用目录Package-1和Package-2in PYTHONPATH,
Package-1/namespace/__init__.py
Package-1/namespace/module1/__init__.py
Package-2/namespace/__init__.py
Package-2/namespace/module2/__init__.py
最终用户可以import namespace.module1和import namespace.module2.
定义命名空间包的最佳方法是什么,因此多个Python产品可以在该命名空间中定义模块?
我有几个python项目,他们都有一个conf包:
/some_folder/project_1/
  conf/
    __init__.py
    some_source_file.py
/another_folder/project_2/
  conf/
    __init__.py
    another_source_file.py
对于每个项目,我在site-packages文件夹中使用以下内容创建了一个.pth文件:
.../site-packages/project_1.pth:
import sys; sys.path.append('/some_folder/project_1/')
.../site-packages/project_2.pth:
import sys; sys.path.append('/another_folder/project_2/')
我可以访问以下模块/some_folder/project_1/:
import conf.some_source_file
但不是以下模块/another_folder/project_2/:
import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'
看起来好像python只搜索conf任何文件夹下面的第一个路径sys.path.有办法解决这个问题吗?