9

I'm wondering about the correct/easiest/most pythonic way of dealing with subprojects that you want have using the same base package. We currently have a file structure like this:

trunk\
    proj1\setup.py
          company_name\__init__.py + proj1's code
    proj2\setup.py
          company_name\__init__.py + proj2's code

We want to keep the namespace company_name common to all our projects (maybe this itself is unpythonic?) but when proj1 and proj2 are installed in develop mode, the first one installed gets broken. It looks like import company_name... gets confused on which company_name package to look in and it grabs the first/last/random one.

How would this normally be handled in a larger python project? Is it possible to resolve this with a setup.py in the trunk that builds some sort of mega-egg? I haven't found any relevant info on google or stack, so any information even just links are greatly appreciated!


edit: I just tried adding a setup.py in the root folder with

...    
namespace_packages = ['company_name'],
package_dir = {'company_name' : ['proj1/company_name', 'proj2/company_name']}
...

with appropriate pkg_resources.declare_namespace(__name__) in the __init_.py files, but ./setup.py bdist_egg just gives:

error in company_name setup command: Distribution contains no modules or packages for namespace package 'company_name'

4

1 回答 1

7

虽然我不能保证我的解决方案的 Python 性,但我最终还是让不同的应用程序可以一起运行。我在命名空间包方面走在了正确的轨道上,但我没有尝试在主干中拥有一个超级项目,而是namespace_packages在每个单独项目的 setup.py 中添加了这一行。这导致一起安装时行为正确,company_name按预期共享命名空间。

任何想插话这是否是一个合理的 python 解决方案的人,我仍然很想知道这是否是“它的完成方式”。感觉不错,但这可能是因为它模仿了我更习惯的 java 风格。

于 2010-08-27T17:35:10.387 回答