1

I am using pycharm version 3.4 on ubuntu and have a project with the following structure:

~/project/src/python/utils/sub1/sub2/sub3/my_code.py

the utils folder also contains an __init__.py file which offers a number of utility functions. I want to include some of them, but it wouldn't find it:

from project.src.python.utils import read_utf8

I followed this post, which seemed to discuss the same problem: PyCharm can't find the right paths if I open a directory that is not the Django root

But changing ~/project to a "source" folder didn't help. This isn't a typo on my side, as it should at least find "project" when trying to import something from it, but

import project

also gives my an "unresolved reference" error.


edit: I should add that I cannot change the code, as it is a shared project. I need that exact import line to work on my machine. My counterpart uses eclipse, were it seems to be easier to add a path to additional code.

Arne
  • 17,706
  • 5
  • 83
  • 99
  • what's the value of `sys.path`? try printing it at the start of the script – GP89 Dec 12 '14 at 23:24
  • also, the syntax of your import statement is wrong, you should separate the paths with dots rather than forward slashes – GP89 Dec 12 '14 at 23:26
  • @GP89 my bad, a typo. It's dots in the code – Arne Dec 12 '14 at 23:32
  • 1
    Try the second answer in the following link: http://stackoverflow.com/questions/24197970/pycharm-import-external-library – Omar Dec 12 '14 at 23:33
  • @Omar thank you very much for pointing me to that answer, the first one actually worked for me, which was a bit less of a hassle – Arne Dec 12 '14 at 23:44

1 Answers1

6

It looks like the source isn't in your PYTHONPATH which is why you're unable to import it.

python only looks in certain places for py/pyc/pyo etc. files and modules to import (the places are the paths listed in sys.path). You can add custom places for it to look with the PYTHONPATH environment variable, or in PyCharm under preferenes > project interpreter > configure interpreters, and then adding any paths in the paths section.

I can't tell which folders are actually modules but from the names of the folders I would guess only utils. If that's the case add the path /home/ceca/project/src/python to the list of paths in PyCharm and in your code from utils import read_utf8

GP89
  • 6,600
  • 4
  • 36
  • 64
  • You are right, except that the free version of pyCharm doesn't include the "configure interpreter" option, as explained in the link offered by Omar. Apart from that, everything is as you said, and my project works now. Thanks a bunch! – Arne Dec 12 '14 at 23:48
  • @ArneRecknagel np. I'm running 3.1- it looks like the preferences might have changed since – GP89 Dec 12 '14 at 23:49
  • huh, what a strange thing to remove. It seems that it's an option which is used quite often, the more visible the better. But what do I know – Arne Dec 12 '14 at 23:55