Tytire Recubans
Tytire Recubans

Reputation: 997

Cannot import module from source directory in pycharm

This question has been asked before - but i could not solve it with any of the answers. I opned a project folder in pycharm (note that the project folder is not in the ../PycharmProject/... directory.

My structure is super basic:

project_title (folder)
             |--src (folder)
                  |-- app.py  
                  |-- pipeline_tools (folder)
                     |-- helpers.py
                     |-- other modules

I tried all possible solutions and combinations but when in app.py i try to import a function from either modules in tools I get errors.

Additionally, Pycharm underlines in red the following statement:

1) from pipeline_tools.helpers import a_certain_function (red underline with "Unresolved reference")

But the following gives me no red:

2) from .pipeline_tools.helpers import a_certain_function (no red underline, note the relative import)

The fact that I have or not a __init__.py inside the modules changes nothing - I still get the following two errors:

For 1)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/gabriele/pycharm-2019.1.1/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'pipeline_tools'

for 2)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/gabriele/pycharm-2019.1.1/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named '__main__.pipeline_tools'; '__main__' is not a package

Stuff I made sure I tried:

1) Go to settings > Project Structure check content root is .../project_title and marked it as "source" - it's blue.

2) Add __init__.py in the pipeline_tools folder and in the /src folder, tried all possible combinations.

3) Use the below code to check the path to the project folder is part of the path string - it is at the end

import sys
print(sys.path)

4) Use the below code to move the project path to the beginning of the sys.path: sys.path.insert(0,'path/to/project_title')

5) Just tried to print PYTHONPATH like this:

os.environ['PYTHONPATH'].split(os.pathsep)

and the directory of my project is not in it

I am a bit at a loss right now. Have no idea what is going on.

Upvotes: 6

Views: 6166

Answers (1)

Tytire Recubans
Tytire Recubans

Reputation: 997

For some frigging reason i don't understand setting the src directory as my source sources root instead of the project_title directory (which contains it anyways...but what do I know) worked.

So solution was:

-> settings -> project structure -> remove previous content root path and make content source the src directory

import like this:

from pipeline_tools.helpers import func_1, func_2

Note: that i do not have a __init__.py file in my folder, as I believe i read they're not necessary anymore.

Prior to Python 3.3, filesystem directories, and directories within zipfiles, had to contain an init.py in order to be recognised as Python package directories. Even if there is no initialisation code to run when the package is imported, an empty init.py file is still needed for the interpreter to find any modules or subpackages in that directory.

This has changed in Python 3.3: now any directory on sys.path with a name that matches the package name being looked for will be recognised as contributing modules and subpackages to that package.

From Nick Coghlan’s Python Notes

Upvotes: 5

Related Questions