在 test.py 中,我正在尝试导入 test_data:
import unittest2
import re
from test_data import receipt1_example
test_data.py 和 test.py 在同一个目录下。我收到以下错误:
/Users/ahammond/.virtualenvs/ric2.6/bin/python2.6 /Applications/PyCharm.app/helpers/pycharm/utrunner.py /Users/ahammond/src/hackfest_spring_2012/parse_me/test.py::test true Testing started at 11:30 AM ... Traceback (most recent call last):
File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line 121, in module = loadSource(a[0]) File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line 44, in loadSource module = imp.load_source(moduleName, fileName) File "/Users/ahammond/src/hackfest_spring_2012/parse_me/test.py", line 4, in from test_data import receipt1_example ImportError: No module named test_dataProcess finished with exit code 1
如您所见,我使用 virtualenv 在 pycharm 下运行它。这是配置的屏幕截图:
最佳答案
我使用的解决方法是:
import sys
import os
try:
import test_data
except ImportError:
sys.path.append(os.path.dirname(__file__))
try:
import test_data
finally:
sys.path.remove(os.path.dirname(__file__))
一位 friend 告诉我,还可以将目录条目添加到一些包含目录中。
关于python - ImportError : No module named test_data, 但 test_data.py 与 PyCharm 下的 test.py 在同一目录中使用 virtualenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437661/