0

I'm running a project in pycharm having the following structure:

main_folder/
    venv
    main.py
    second_folder/
        module1.py
        third_folder/
            __init__.py
            module2.py
            module3.py

I'm trying to import modules 2 & 3 from within module 1 as it follows:

#in third_folder/__init__.py
from module2 import *
from module3 import *
#in second_folder/module1.py
from third_folder import *

module2.function()

I've been trying to follow the answers to older questions (such as this) but it doesn't seem to apply in my case.

I'm getting the following:

from module2 import *
ModuleNotFoundError: No module named 'module2'

I'm sure the solution is pretty trivial, but I can't manage to figure it out.

Thanks in advance.

The Doctor
  • 17
  • 5

1 Answers1

1

You need a dot to indicate here package::

#in third_folder/__init__.py
from .module2 import *
from .module3 import *

again::

#in second_folder/module1.py
from .third_folder import *

Pycharm is a bit weird when dealing with PYTHONPATH, you will need to decide what is the root of your project and set it as SOURCE_ROOT which translates to PYTHONPATH, and you need to make sure your CURRENT WORK DIR in the RUN Config to be what you selected as SOURCE ROOT

Meitham
  • 9,178
  • 5
  • 34
  • 45