简体   繁体   English

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

[英]ImportError: No module named test_data, but test_data.py in same directory as test.py under PyCharm using virtualenv

In test.py, I am trying to import test_data: 在test.py中,我试图导入test_data:

import unittest2
import re

from test_data import receipt1_example

test_data.py is in the same directory as test.py. test_data.py与test.py位于同一目录中。 I get the following error: 我收到以下错误:

/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): /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(最近一次电话会议):
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_data 文件“/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的模块

Process finished with exit code 1 进程以退出代码1结束

As you can see, I am running this under pycharm using a virtualenv. 如您所见,我使用virtualenv在pycharm下运行它。 Here's a screenshot of the configuration: 这是配置的屏幕截图:

PyCharm调试配置

The work around i use is: 我使用的工作是:

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__))

A friend told me that one can also add the directory entries to some include directories. 一位朋友告诉我,还可以将目录条目添加到某些包含目录中。

Please try PyCharm 2.5.1 RC , there was a bug with sys.path building (it contained incorrect, duplicated project source directory). 请尝试PyCharm 2.5.1 RCsys.path构建有一个错误(它包含错误的,重复的项目源目录)。

If it's not the case, you can mark additional directories as Source in Preferences | 如果不是这种情况,您可以在Preferences |中将其他目录标记为Source Project Structure or add them to the Paths in the Python Interpreters . Project Structure或将它们添加到Python Interpreters路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM