我的python以某种方式无法在同一目录中找到任何模块。我究竟做错了什么?(python2.7)
所以我有一个目录“ 2014_07_13_test”,其中有两个文件:
在hello.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
def hello1():
print 'HelloWorld!'
和test.py:
# !/usr/local/bin/python
# -*- coding: utf-8 -*-
from hello import hello1
hello1()
还是python给了我
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 4, in <module>
ImportError: No module named hello
怎么了?
您的代码很好,我怀疑您的问题是如何启动它。
您需要从“ 2014_07_13_test”目录启动python。
打开命令提示符,然后将“ cd”进入您的“ 2014_07_13_test”目录。
例如:
$ cd /path/to/2014_07_13_test
$ python test.py
如果您无法像这样“ cd”进入目录,则可以将其添加到sys.path
在test.py中:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
或设置/编辑PYTHONPATH
一切都应该很好...
...嗯,'shebang'行(两个文件的第一行)都存在一个小错误,'#'和'!'之间不应有空格。
有一个更好的家当,你应该使用。
同样,您不需要每个文件上的shebang行……仅是您打算从Shell作为可执行文件运行的文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句