Python在同一文件夹中找不到模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24722212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python can't find module in the same folder
提问by Philipp_Kats
My python somehow can't find any modules in the same directory. What am I doing wrong? (python2.7)
我的 python 不知何故在同一目录中找不到任何模块。我究竟做错了什么?(python2.7)
So I have one directory '2014_07_13_test', with two files in it:
所以我有一个目录“2014_07_13_test”,里面有两个文件:
- test.py
- hello.py
- 测试文件
- 你好.py
where hello.py:
你好.py在哪里:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
and test.py:
和 test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
Still python gives me
仍然蟒蛇给了我
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
What's wrong?
怎么了?
采纳答案by Jeremy Allen
Your code is fine, I suspect your problem is how you are launching it.
你的代码很好,我怀疑你的问题是你如何启动它。
You need to launch python from your '2014_07_13_test' directory.
您需要从“2014_07_13_test”目录启动 python。
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
打开命令提示符并“cd”进入“2014_07_13_test”目录。
For instance:
例如:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
如果你不能 'cd' 进入这样的目录,你可以将它添加到sys.path
In test.py:
在 test.py 中:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
或者设置/编辑PYTHONPATH
And all should be well...
一切都应该很好......
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
...好吧,您的“shebang”行(两个文件中的第一行)有一个小错误,“#”和“!”之间不应该有空格。
There is a better shebangyou should use.
有一个更好的家当,你应该使用。
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
此外,您不需要每个文件上的 shebang 行......只需要您打算从 shell 作为可执行文件运行的那些。
回答by jfn
Change your import in test.py to:
将 test.py 中的导入更改为:
from .hello import hello1
回答by ecotner
I had a similar problem, I solved it by explicitly adding the file's directory to the path list:
我有一个类似的问题,我通过将文件的目录显式添加到路径列表来解决它:
import os
import sys
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
After that, I had no problem importing from the same directory.
之后,我从同一目录导入没有问题。
回答by apadana
Here is the generic solution I use. It solves the problem for importing from modules in the same folder:
这是我使用的通用解决方案。它解决了从同一文件夹中的模块导入的问题:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Put this at top of the module which gives the error "No module named xxxx"
把它放在模块的顶部,它会给出错误“没有名为 xxxx 的模块”
回答by Chris Claxton
In my case, Python was unable to find it because I'd put the code inside a module with hyphens, e.g. my-module
. When I changed it to my_module
it worked.
就我而言,Python 无法找到它,因为我将代码放在带有连字符的模块中,例如my-module
. 当我将其更改为它时,my_module
它起作用了。
回答by Nippon87
I ran into this issue. I had three folders in the same directory so I had to specify which folder. Ex: from Folder import script
我遇到了这个问题。我在同一个目录中有三个文件夹,所以我必须指定哪个文件夹。例如:从文件夹导入脚本