2012-05-03 8 views
6

In test.py, sto cercando di importare test_data:ImportError: No module named test_data, ma test_data.py nella stessa directory test.py sotto PyCharm utilizzando virtualenv

import unittest2 
import re 

from test_data import receipt1_example 

test_data.py è nella stessa directory di test.py. Ottengo il seguente errore:

/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

Come potete vedere, sto facendo funzionare questo sotto PyCharm utilizzando un virtualenv. Ecco uno screenshot della configurazione:

PyCharm debug configuration

risposta

4

Il lavoro attorno che uso è:

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

Un amico mi ha detto che si può anche aggiungere le voci di directory per alcuni includono directory.

+0

Brutto, ma funziona. Grazie! Er, ovviamente usando da test_data import receipt1_example – Andrew

3

Si prega di provare PyCharm 2.5.1 RC, c'era un bug con sys.path edificio (conteneva directory di origine del progetto errato, duplicato).

Se non è il caso, è possibile contrassegnare directory aggiuntive come Source in Preferences | Project Structure o aggiungili ai percorsi nello Python Interpreters.

+0

Ho aggiornato a pycharm-117.296.dmg e ho ancora lo stesso errore. Post scriptum Voi ragazzi siete fantastici. – Andrew

+0

si blocca su "prove di istanziazione per me :(" – pistacchio

+1

In realtà ho lo stesso problema con PyCharm 2.7 – Rmatt