0

I have the following simplified repo structure:

- project
  - src
    - config
      - config_new.py
    - util
      - util_new.py
    - app
      - app_new.py

The app_new.py module looks like the following (it is the core module to be executed via python app_new.py):

from project.src.config.config_new import ConfigClass
from project.src.util.util_new import UtilClass

def func1():
   ...

if __name__ == "__main__":
   func1()

After my old repo had to be closed, I cloned the new repo version into my PyCharm project folder and associated my pre-existing virtual environment with the new repo. However, after cloning, the relative imports do suddenly not work anymore across the repo and the following error is thrown:

from project.config import config_new.py    
ModuleNotFoundError: No module named "config_new.py"

I have literally tried anything:

  • Cleared PyCharm cache
  • Checked interpreter paths
  • Inputted init.py files into the root directory and all subdirectories
  • Cloned the repo multiple times
  • Marked all directories above as source directories
  • Experimented with double dots on the relative imports to refer to top-level directories
  • Checked this helpful thread and all related threads: Relative imports for the billionth time

As far as I have understood, when using a module with if name == "main":, the relative import is not considered to be in a package. However, I could use this exact setup before cloning the repo again and re-associating the existing virtual environment with it. Is there anything else I might have overlooked? Thanks!

29nivek
  • 35
  • 7
  • 1
    Have you tried running from outside the `project` folder with `python project/src/app/app_new.py`? – Axe319 Mar 21 '23 at 17:16
  • I just tried it and the error persists unfortunately – 29nivek Mar 21 '23 at 17:45
  • `from project.config import config_new.py` shouldn't this read from project.src.config? Are you running it in Pycharm? Can you post your run configuration? – Mr_and_Mrs_D Mar 22 '23 at 23:02

1 Answers1

1

You may be trying to import using the wrong path.

If this:

- project
  - src
    - config
      - config_new.py
    - util
      - util_new.py
    - app
      - app_new.py

is your project structure, the path to the scripts shouldn't be project.config.config_new.py but project.src.config.config_new.py. Therefore you would need to change your import statements from from project.config import config_new.py to from project.src.config import config_new.py.

lstuma
  • 31
  • 4
  • Thank you for your swift reply and I am sorry, I just posted an incomplete import statement here. It is now fixed. – 29nivek Mar 21 '23 at 17:12