简体   繁体   English

ImportError:导入我自己的子包时没有名为“”的模块

[英]ImportError: No module named '' when importing my own sub-package

By following the instructions in this SO answer I've created a Python package with sub-package as sub-folders, each with a __init__.py file (which are all totally empty). 按照这个SO答案中的说明,我创建了一个Python包,其中的子包作为子文件夹,每个子文件夹都带有__init__.py文件(它们全部为空)。

  top_module
     __init__.py
     module_a.py
        sub_module
             __init__.py
             module_c.py

I can import the top level module but trying to import a sub-module results in an ImportError : 我可以导入顶级模块,但是尝试导入子模块会导致ImportError

>>> import top_module
>>> import top_module.sub_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named sub_module

In iPython I can autocomplete top_model. 在iPython中,我可以自动完成top_model. to show me module_a.py but not sub_module . 给我module_a.py但不sub_module

Followed the instructions in this SO answer but I just get: 遵循此SO答案中的说明,但我得到:

>>> top_module.__file__
'top_module.pyc'

which is not terribly useful. 这不是非常有用。 Interestingly, __package__ gives me: 有趣的是, __package__给了我:

>>> print top_module.__package__
None

I can do this: 我可以做这个:

>>> import top_module
>>> import sub_module.module_c

So why not import top_module.sub_module.module_c ? 那么为什么不import top_module.sub_module.module_c

I've worked out what my problem was (and it's a pretty dumb error I'm afraid.) 我已经解决了我的问题(恐怕这是一个非常愚蠢的错误)。

I had my PYTHONPATH set to 我的PYTHONPATH设置为

/path/to/top_module

and was doing 并且在做

import module_a

which of course worked. 当然有效。 But trying to do 但是尝试去做

import top_module.submodule

didn't work because the PYTHONPATH didn't "know" about top_module , it was already in top_module . 因为PYTHONPATH不“了解” top_module ,所以它top_module ,它已经在 top_module

尝试from sub_module.module_c import *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入我自己的模块时出现“ImportError: No module named...” - “ImportError: No module named…” when importing my own module 导入子包时,wxPython两次启动我的应用程序 - wxPython launches my app twice when importing a sub-package Python:从子包和主包导入子包模块 - Python: importing a sub-package module from both the sub-package and the main package 与站点包冲突时,从当前目录中的子包导入模块 - Importing module from sub-package in current directory when it clashes with site-packages 导入完整的包或子包而不是包内的模块是什么意思? - What is the meaning of importing a complete package or a sub-package and not a module inside the package? 导入Python包-“ ImportError:未命名模块...” - Importing Python package - “ImportError: No module named…” ImportError:导入dogtail时没有名为gconf的模块 - ImportError: No module named gconf when importing dogtail ImportError导入袖扣时没有名为“ plotlytools”的模块 - ImportError No module named 'plotlytools' when importing Cufflinks ImportError:导入mechanize时没有名为'_version'的模块 - ImportError: No module named '_version' when importing mechanize Pip 在 python 中从自己的(命名空间)package 安装子包 - Pip install sub-package from own (namespace) package in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM