Python工作包结构不简单

2024-04-29 14:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的环境是virtualenv和python2.7

我创建了一个非常简单的python包来尝试识别问题。我有一个包含子包的包,所有的子包都带有一个__init__.py文件,但是在构建之后,我无法从子包中导入文件。所有的文件都是空的,除了我要导入的文件,它只包含一个伪类。在

class LazyUrl(object): pass。在

包结构

- setup.py
- sloth_toolkit/
        - __init__.py
        - webtools/
                - __init__.py
                - urls.py
        - systools/
                - __init__.py
        - utils/
                - __init__.py

在设置.py

^{2}$

在网址.py

class LazyUrl(object):
    pass

然后安装包,将终端移动到用户根目录以避免导入源代码,然后运行ipython。我导入包没有问题,然后尝试导入/访问虚拟类LazyUrl,这就是它中断的地方。在

ipython会话

In [1]: import sloth_toolkit

In [2]: sloth_toolkit.webtools.urls.LazyUrl()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-edc6d4b8bbf3> in <module>()
----> 1 sloth_toolkit.webtools.urls.LazyUrl()

AttributeError: 'module' object has no attribute 'webtools'

In [3]: from sloth_toolkit.webtools import urls
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-f6b31fa7f72c> in <module>()
----> 1 from sloth_toolkit.webtools import urls

ImportError: No module named webtools

这让我发疯了。我相信问题是我的环境放在我不知道。在

这是我正在做的项目https://github.com/crispycret/sloth-toolkit。在

在virtualenv中安装并导入包之后,我从将LazyUrl类导入到packages main__init__.py文件时得到这个错误。在

实包错误

In [1]: import sloth_toolkit
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-70e153ca48f0> in <module>()
----> 1 import sloth_toolkit

/home/crispycret/Documents/sloth-testing/lib/python2.7/site-packages/sloth_toolkit-0.0.11-py2.7.egg/sloth_toolkit/__init__.py in <module>()
      3 # from . import utilities
      4 
----> 5 from .webtools.urls import LazyUrl
      6 from .systools.paths import LazyPath
      7 

ImportError: No module named webtools.urls

Tags: 文件infrompyimportinitipythonurls