[英]ImportError: No module named … (but I have __init__.py files and have set PYTHONPATH)
I know this question is asked over and over, but looking at all the similar questions here I cannot seem to find any answers that are much different that what I have already tried... Apologies if this really is a duplicate. 我知道这个问题是一遍又一遍地问的,但是在这里查看所有类似的问题,我似乎找不到任何与我已经尝试过的答案大不相同的答案……很抱歉,如果真的是重复的话。
I have a simple python package that have written called "clam". 我有一个简单的python程序包,称为“蛤”。 But when I try to import a module from it, I get the ImportError: no module named ... error.
但是,当我尝试从中导入模块时,出现ImportError:没有名为...错误的模块。
But if I directly import the module I do not get an error: 但是,如果我直接导入模块,则不会出现错误:
import libClam <-- this works.
I have looked at a number of similar questions here to see what I am doing wrong but they all seem to suggest doing what I have already done (set my PYTHONPATH to point to the parent directory of the module I am trying to import AND to add an empty file named: 我在这里查看了许多类似的问题,以查看我做错了什么,但它们似乎都建议执行我已经做过的事情(将PYTHONPATH设置为指向我要导入并添加的模块的父目录一个空文件,名为:
__init__.py
that lives in that directory). 位于该目录中)。 I have also poured over this page:
我也倒在这个页面上:
https://docs.python.org/2/tutorial/modules.html https://docs.python.org/2/tutorial/modules.html
(specifically the section 6.4 Packages) (特别是第6.4节包)
Here is a short output from my shell: 这是我的shell的简短输出:
bvz@manjaro-xps15:~/Documents/dev/clam$ pwd
/home/bvz/Documents/dev/clam
bvz@manjaro-xps15:~/Documents/dev/clam$ ls -l
total 56
-rwxrwxrwx 1 bvz users 348 Apr 7 14:48 clamError.py
-rw-r--r-- 1 bvz users 769 Apr 7 14:48 clamError.pyc
-rw-r--r-- 1 bvz users 0 Apr 10 22:23 __init__.py
-rw-r--r-- 1 bvz users 127 Apr 3 20:10 __init__.pyc
-rwxrwxrwx 1 bvz users 7782 Apr 10 22:32 libClam.py
-rw-r--r-- 1 bvz users 7006 Apr 10 22:32 libClam.pyc
-rwxrwxrwx 1 bvz users 13602 Apr 10 19:06 libClarisse.py
-rw-r--r-- 1 bvz users 11585 Apr 10 22:19 libClarisse.pyc
-rwxrwxrwx 1 bvz users 0 Apr 7 12:28 parking.py
bvz@manjaro-xps15:~/Documents/dev/clam$ cat __init__.py
bvz@manjaro-xps15:~/Documents/dev/clam$ env | grep PYTHONPATH
PYTHONPATH=/home/bvz/Documents/dev/squirrel:/home/bvz/Documents/dev/clam:
bvz@manjaro-xps15:~/Documents/dev/clam$ python2
Python 2.7.15 (default, Jan 10 2019, 23:20:52)
[GCC 8.2.1 20181127] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/bvz/Documents/dev/squirrel', '/home/bvz/Documents/dev/clam', '/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-39.1.0-py2.7.egg']
>>> import clam.libClam
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named clam.libClam
>>> from clam import libClam
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named clam
>>> import libClam
>>>
Notice the last line where the simple statement: 注意最后一行简单的语句:
import libClam
works. 作品。 So apparently I have successfully set my PYTHONPATH, but somehow it does not recognize the parent directory as a package.
因此,显然我已经成功设置了PYTHONPATH,但是不知何故它无法将父目录识别为程序包。
python 3.7 is the default installation on this machine (ie if I type 'python' I get a python 3.7 session). python 3.7是此计算机上的默认安装(即,如果我键入“ python”,我将获得python 3.7会话)。 But this code needs to run in a DCC package that is still limited to python 2.7.
但是此代码需要在仍限于python 2.7的DCC程序包中运行。 Could that be part of the issue?
这可能是问题的一部分吗?
I am sorry for the very basic nature of this question, but even trying to do all of my due diligence and research I am coming up with a blank. 对于这个问题的基本性质,我感到很抱歉,但是即使试图进行我所有的尽职调查和研究,我仍然空白。 Thanks.
谢谢。
Imports should be relative to the directory you run Python from. 导入应相对于您运行Python的目录。 Since your current working directory is already inside
clam
, you should not include it in your import. 由于您当前的工作目录已经在
clam
,因此您不应在导入中包含该目录。 Instead, do: 相反,请执行以下操作:
import libClam
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.