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:
- Please don't mark it as duplicate before reading it: I looked at many other answers and none works.
- Please don't suggest writing
This only solves the problem for main.py, but I have to import src.utils.configs from other files (like other_module.py)from utils.configs import read_configs - 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

