导入Python命名空间包的问题 [英] Import problems with Python namespace packages

查看:603
本文介绍了导入Python命名空间包的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python 。 / p>

基本上,您将删除两个存储库中的 __ init __。py 文件,并添加:

  setup(
...
packages = ['coollibrary。{foomodule / barmodule}'],
namespace_packages = ['coollibrary'],
...

给你 setup.py



虽然不能帮助你使用Python 2.7 ...


I am trying to use Python namespace packages concept to split my library across multiple directories. In general, it works, but I have a problem regarding importing names to projects package level.

My project structure is following:

project1/coollibrary/__init__.py

from __future__ import absolute_import

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

from .foomodule import foo

project1/coollibrary/foomodule.py

def foo():
    print ('foo')

project2/coollibrary/__init__.py

from __future__ import absolute_import

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

from .barmodule import bar

project2/coollibrary/barmodule.py

def bar():
    print ('bar')

Both projects are in the PATH:

$ echo ${PYTHONPATH}
/home/timo/Desktop/example/project1:/home/timo/Desktop/example/project2

And I am running code from here:

$ pwd
/home/timo/Desktop/example

$ python3
>>> import coollibrary
>>> coollibrary.foo() # works
foo
>>> coollibrary.bar() # does not work (the problem)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'bar'
>>> import coollibrary.barmodule 
>>> coollibrary.barmodule.bar() # works
bar

How to fix the code so that I could import both foo and bar directly from coollibrary package. Also, is there a solution that works for both Python2.7 and Python3.4 (other versions not required).

解决方案

Starting with Python 3.3, you can use PEP 420 -- Implicit Namespace Packages.

Basically, you would remove your __init__.py file in both repositories, and add:

setup(
    ...
    packages=['coollibrary.{foomodule/barmodule}'],
    namespace_packages=['coollibrary'],
    ...
)

to your setup.py.

Can't help you with Python 2.7 though...

这篇关于导入Python命名空间包的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆