6

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.

1 Answer 1

5

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

4
  • 3
    If project_title was your source your imports should look like: from src.pipeline_tools.helpers import myfunction. As you write your imports as from pipeline_tools.... you implicitly assume that you sources root is src, that's why when you define src as such` it works. One quick way of setting this up is to right click on the folder you want to be you sources root and to click Mark directory as -> Sources root. The directory should then appear in blue color in the tree.
    – Eskapp
    May 22, 2019 at 13:05
  • Thanks a lot for this explanation! "As you write your imports as from pipeline_tools.... you implicitly assume that you sources root is src" Does this mean that with project_title as sources I could have simply written from helpers import func? May 22, 2019 at 13:20
  • 1
    No, you need to precise the path from the root you choose. So if it is the top folder, you would need to write from src.pipeline_tools.helpers import func. However, I suggest that you choose src for the Sources Root as it seems more natural given how the tree looks like.
    – Eskapp
    May 22, 2019 at 13:23
  • This worked for me. Setting the src directory as the Sources folder is what I needed.
    – Kent Bull
    May 17 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.