[英]Python ImportError / ModuleNotFoundError on own package in Eclipse PyDev
Solved, thanks! 解决了,谢谢!
Let's say I have a self-written module located under C:\\mymodules\\general
which contains the files foo.py
and __init__.py
. 假设我有一个位于
C:\\mymodules\\general
下的自写模块,其中包含文件foo.py
和__init__.py
。
Now I want to import the function bar()
, which is located inside foo.py
, into a script in a completely different place. 现在,我想将位于
foo.py
内的功能bar()
导入到脚本中一个完全不同的位置。
Why is this not working? 为什么这不起作用?
import sys
sys.path.append(r"C:\mymodules")
from general import foo
foo.bar()
I get ImportError: cannot import name 'foo'
我收到
ImportError: cannot import name 'foo'
The same if I add C:\\mymodules\\general
to the path, instead. 如果将
C:\\mymodules\\general
到路径,则相同。
Alternatively, I have also tried 另外,我也尝试过
import sys
sys.path.append(r"C:\mymodules")
import general.foo
foo.bar()
Here, I get ModuleNotFoundError: No module named 'general.foo'; 'general' is not a package
在这里,我得到
ModuleNotFoundError: No module named 'general.foo'; 'general' is not a package
ModuleNotFoundError: No module named 'general.foo'; 'general' is not a package
. ModuleNotFoundError: No module named 'general.foo'; 'general' is not a package
。
Why would general not be a package? 为什么一般不打包? I thought the requirement was "contains an
__init__.py
" (and the module I want to import, of course)? 我以为要求是“包含
__init__.py
”(当然还有我要导入的模块)?
This is all Python3, using PyDev in Eclipse under Windows7. 这就是所有Python3,在Windows7下的Eclipse中使用PyDev。
Can anyone tell me what's wrong and how to do it instead? 谁能告诉我什么地方出了问题以及怎么做呢?
Edit: the file is, indeed, already called __init__.py
, so that is not the problem. 编辑:该文件确实已经名为
__init__.py
,所以这不是问题。
__init__.py
already contains the line __init__.py
已经包含该行
__all__ = ["foo"]
Edit 2: Weirdly enough, the following works: 编辑2:奇怪的是,以下作品:
import sys
sys.path.append(r"C:\mymodules")
from general import *
bar()
I really don't want to do import *
, though. 不过,我真的不想
import *
。 Surely there must be a cleaner way? 当然必须有一种更清洁的方法吗?
Edit 3: When I run it from IDLE, it works! 编辑3:当我从IDLE运行它时,它起作用了! (The first code, that is.) But in Eclipse PyDev, I still get the same error.
(就是第一个代码。)但是在Eclipse PyDev中,我仍然遇到相同的错误。 Why?
为什么?
Ah! 啊! Solution found!
找到解决方案! (see answer below, to close this).
(请参见下面的答案以关闭此操作)。
Solution found. 找到解决方案。 headdesk
headdesk
In PyDev's Pythonpath (configured for the whole workspace), I had another package that contains a module called general.py
. 在PyDev的Pythonpath(为整个工作区配置)中,我有另一个软件包,其中包含一个名为
general.py
的模块 。 The interpreter found this before it got to my package general
, so the message general is not a package
referred to the module of the same name. 解释器在到达我的package
general
之前就找到了这个消息,因此message general is not a package
指向同名模块的general is not a package
。
That should teach me to use separate workspaces when I do need to reuse existing names. 当我确实需要重用现有名称时,这应该教会我使用单独的工作区。 And ideally, not to use the same name for different things, anyway.
理想情况下,无论如何不要在不同的事物上使用相同的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.