Arne
Arne

Reputation: 20157

Finding import path in pycharm

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.

Upvotes: 2

Views: 9447

Answers (1)

GP89
GP89

Reputation: 6730

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

Upvotes: 9

Related Questions