ImportError: No module named test_data, but test_data.py in same directory as test.py under PyCharm using virtualenv
Asked Answered
C

2

6

In test.py, I am trying to import test_data:

import unittest2
import re

from test_data import receipt1_example

test_data.py is in the same directory as 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):
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

Process finished with exit code 1

As you can see, I am running this under pycharm using a virtualenv. Here's a screenshot of the configuration:

PyCharm debug configuration

Coble answered 3/5, 2012 at 18:56 Comment(0)
V
4

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.

Volitant answered 3/5, 2012 at 19:18 Comment(1)
Ugly, but works. Thanks! Er, obviously using from test_data import receipt1_exampleCoble
C
3

Please try PyCharm 2.5.1 RC, there was a bug with sys.path building (it contained incorrect, duplicated project source directory).

If it's not the case, you can mark additional directories as Source in Preferences | Project Structure or add them to the Paths in the Python Interpreters.

Cluster answered 3/5, 2012 at 19:27 Comment(4)
I updated to pycharm-117.296.dmg and still have the same error. P.S. You guys are awesome.Coble
it hangs at " instantiating tests for me :( "Shockey
I actually do have the same problem with PyCharm 2.7Deluca
Me too :/ Still awesome IDE though.Glomerulus

© 2022 - 2024 — McMap. All rights reserved.