简体   繁体   English

Python奇怪的导入行为:导入xy工作,从x import y没有

[英]Python strange import behavior: import x.y works, from x import y doesn't

I am having a simple directory structure, like that: 我有一个简单的目录结构,像这样:

MyProject
--main.py
--lib #that's a directory/package
----__init__.py
----view.py
----common_lib.py
----other irrelevant modules...

In main.py: 在main.py中:

from lib import view

causes the following error: 导致以下错误:

ImportError: cannot import name view

If instead, I write it like: 如果相反,我写它像:

from lib.view import *

This import passes successfully, but next failure happens in view.py, in that: 此导入成功传递,但在view.py中发生下一次失败,其中:

from common_lib import Comments, Locations, ScreenData, ProgressSignal

causes: 原因:

ImportError: No module named 'common_lib'

And as it appears from the directory structure, common_lib.py resides in the same directory as view.py, how can it happen that it cannot be found? 从目录结构看,common_lib.py与view.py位于同一目录中,怎么会发现它无法找到呢? How does it come that 'from x import y' doesn't work, and 'from xy import *' works? 如何从'x import y'起作用,'from xy import *'有效吗? ' __init__.py ' is completely empty BTW. ' __init__.py '完全是空的BTW。 And I am using Python 3.3 32-bit. 我正在使用Python 3.3 32位。

What is more annoying, this same program worked 2 days ago.I was testing some code in IDLE and when I thought the code was ready to include in the project, pasted it into PyDev, I was shocked by this error. 更令人讨厌的是,这个程序工作2天前。我正在测试IDLE中的一些代码,当我认为代码已准备好包含在项目中时,将其粘贴到PyDev中,我对此错误感到震惊。 I changed nothing about my directories or modules. 我没有改变我的目录或模块。

Also, still more strangeness, running view.py and common_lib.py as standalone (without being imported) runs just fine. 此外,更奇怪的是,运行view.py和common_lib.py作为独立(没有导入)运行就好了。 It should produce the error if any issues really existed. 如果确实存在任何问题,它应该产生错误。

Thanks. 谢谢。 Any advice is highly appreciated. 任何建议都非常感谢。

Since main.py is still at the top level, you need to use lib.common_lib : 由于main.py仍处于顶层,因此您需要使用lib.common_lib

from lib.common_lib import Comments, Locations, ScreenData, ProgressSignal

because the previous line from lib import view does not start looking for modules from inside lib . 因为from lib import view中的上一行没有开始从lib内部查找模块。

Given: 鉴于:

+--main.py     # from lib import view
+--lib
   +--__init__.py
   +--common_lib.py # Comments, etc.
   +--view.py       # from .common_lib import Comments, etc.

This works: 这有效:

from lib import view

And this works from view.py with a relative import to indicate common_lib is in the same package. 这可以在view.py使用相对导入来指示common_lib在同一个包中。

from .common_lib import Comments, Locations, ScreenData, ProgressSignal

Works for me: 适合我:

danielallan@MacBook:~$mkdir myproject
danielallan@MacBook:~$cd myproject/
danielallan@MacBook:myproject$mkdir lib
danielallan@MacBook:myproject$cd lib
danielallan@MacBook:lib$touch __init__.py
danielallan@MacBook:lib$touch view.py
danielallan@MacBook:lib$touch common_lib.py
danielallan@MacBook:lib$cd ..

In [1]: from lib import view

In [2]: view
Out[2]: <module 'lib.view' from 'lib/view.pyc'>

What happens when you try that on your machine? 在机器上尝试时会发生什么? Are you sitting in the wrong directory, or is your path not configured to find these files? 您是否坐在错误的目录中,或者您的路径未配置为查找这些文件?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM