ImportError:在使用virtualenv的PyCharm下没有名为test_data的模块,但是test_data.py与test.py在同一目录中

And*_*rew 6 python import virtualenv pycharm unittest2

在test.py中,我试图导入test_data:

import unittest2
import re

from test_data import receipt1_example
Run Code Online (Sandbox Code Playgroud)

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上午11:30开始... Traceback(最近一次调用最后一次):
文件"/Applications/PyCharm.app/helpers/pycharm/utrunner.py",第121行,在module = loadSource(a [0])文件中/Applications/PyCharm.app/helpers/pycharm/utrunner.py",第44行,在loadSource模块中= imp.load_source(moduleName,fileName)文件"/Users/ahammond/src/hackfest_spring_2012/parse_me/test.py",行4,从test_data导入receipt1_example ImportError:没有名为test_data的模块

进程以退出代码1结束

如您所见,我使用virtualenv在pycharm下运行它.这是配置的屏幕截图:

PyCharm调试配置

Use*_*ser 4

我使用的解决方法是:

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__))
Run Code Online (Sandbox Code Playgroud)

一位朋友告诉我,还可以将目录条目添加到某些包含目录中。