我试图导入一个实用程序文件,但只有在通过脚本运行代码时才会遇到奇怪的错误。ImportError:没有名为utils的模块
当运行test.py
位置:/home/amourav/Python/proj/test.py
代码:
import os
os.chdir(r'/home/amourav/Python/')
print os.listdir(os.getcwd())
print os.getcwd()
from UTILS import *
的输出是:
['UTILS_local.py','UTILS.py', 'proj', 'UTILS.pyc']
/home/amourav/Python
Traceback (most recent call last): File "UNET_2D_AUG17.py", line 11, in from UTILS import * ImportError: No module named UTILS
但是当我通过bash终端运行代码时,它似乎工作正常
bash-4.1$ python
>>> import os
>>> os.chdir(r'/home/amourav/Python/')
>>> print os.listdir(os.getcwd())
['UTILS_local.py','UTILS.py', 'proj', 'UTILS.pyc']
>>> from UTILS import *
blah blah -everything is fine- blah blah
我在Linux机器上运行的Python 2.7.10
你能否详细说明为什么会发生这种情况? –
^^我试图重现它,但我失败了。 Python 2.7.12 – User
认为它工作了一秒,但没有。 python ./test.py - >导入错误 –