ModuleNotFoundError but path is in sys.path

Viewed 31

I have the following project structure:

my-project
  src
    __init__.py
    main.py
    utils
      __init__.py
      configs
    modules
      __init__.py
      other_module.py

My main.py script is as follows:

from pathlib import Path
import sys, os
print("cwd: {}".format(os.getcwd()))
# appending directory to sys.path
sys.path.append(os.getcwd())
# printing to check if it worked
for p in sys.path:
    print(p)
from src.utils.configs import read_config

if __name__=='__main__':
   # do stuff
   pass

The run keeps failing with ModuleNotFoundError: No module named 'src.utils'

I think this could be due to another module being called src. Below my verbose output:

import 'pathlib' # <_frozen_importlib_external.SourceFileLoader object at 0x000001CC4C31B370>
cwd: C:\Users\UserName\LOCAL\my-project
C:\Users\UserName\LOCAL\my-project\src
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\python310.zip
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\DLLs
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib
C:\Users\UserName\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0
C:\Users\UserName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib\site-packages
C:\Users\UserName\LOCAL\my-project
# C:\Users\UserName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\src\__pycache__\__init__.cpython-310.pyc matches C:\Users\UserName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\src\__init__.py
# code object from 'C:\\Users\\UserName\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\src\\__pycache__\\__init__.cpython-310.pyc'
import 'src' # <_frozen_importlib_external.SourceFileLoader object at 0x000001CC4C147D60>
Traceback (most recent call last):
  File "C:\Users\UserName\LOCAL\my-project\src\nats_minimal_listener.py", line 7, in <module>
    from src.utils.configs import read_config
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'src.utils'

A few points:

  1. Please don't mark it as duplicate before reading it: I looked at many other answers and none works.
  2. Please don't suggest writing
     from utils.configs import read_configs
    
    This only solves the problem for main.py, but I have to import src.utils.configs from other files (like other_module.py)
  3. Why rearranging the file structure is not practical: If I want to import a "configs" module from all submodules, this would need to be a subfolder of all submodules and would need to be duplicate in all submodules, like this:
my-project
  src
    __init__.py
    main.py
    utils
      __init__.py
      configs
    module1
      __init__.py
      other_module.py
      utils
        __init__.py
        configs
     module2
      __init__.py
      other_module.py
      utils
        __init__.py
        configs
1 Answers

Please check on this format enter image description here

I hope the folder structure and nature of imports help you relate the imports It's best you re-organise your codebase to make the imports a bit easier enter image description here

To make this clear src is the root folder as you have described Meaning once you add other modules in the src folder. they will be relative to the src folder Adding __init__.py in the src makes it a module and the other submodules under it should have __init__.py in them will be relative already so a file like main.py in the src you can import using from module.submodule import module instead of from src.module.submodule import module So in your imports don't use the src....

Related