The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping

--

Chapters
00:00 Importerror: No Module Named Test_data, But Test_data.Py In Same Directory As Test.Py Under Pycharm
01:08 Accepted Answer Score 4
01:27 Answer 2 Score 3
01:43 Thank you

--

Full question
https://stackoverflow.com/questions/1043...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #import #virtualenv #pycharm #unittest2

#avk47



ACCEPTED ANSWER

Score 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.




ANSWER 2

Score 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.