'ModuleNotFoundError for Python local packages
This question has been asked many times but none of those solutions seem to work for my specific usecase (trying PYTHONPATH, absolute imports with init.py, etc).
I have a folder structure like this.
app.py
mainFunctions
-----__init__.py
------main.py
apiFunctions
-----__init__.py
-----apiFunc.py
-----apiLib
-----------__init__.py
-----------api.py
In the root of the project, I have app.py. My flask run triggers from there.
#app.py
from mainFunctions import myFunc
#mainFunctions/__init__.py
from .main, import *
This part works. when myFunc is called in app.py (I use flask since its a flask app), my function is called. Here is where the problem occurs.
#mainFunctions/main.py
from apiFunctions import getApiData
def myFunct():
myVar = getApiData()
##do something
#apiFunctions/__init__.py
from .apiFunc import *
from apiLib import *
#apiLib/__init__.py
from .api import *
#apiFunctions/apiLib/api.py
def getApiData():
##gets API data
I keep getting ModuleNotFoundError: No module named apiFunctions.
I tried sys.path.append and found the folder using:
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))
That didnt work. Neither did adding PYTHONPATH in my vsCode settings.json using:
"terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"
Any idea on why apiFunctions isn't being recognized by main.py even though I have a __init__.py file with all the absolute imports? I know the issue has to do with the path not being recognized but cannot wrap my head around how to resolve this issue.
Solution 1:[1]
The problem should really be caused by the import problem between directory levels. Could you provide a complete error reporting? I reproduced your code and found that it was inconsistent with the error you generated.Here is my error:
ModuleNotFoundError: No module named 'apiLib'
And my solution is change #apiFunctions/__init__.py:
from .apiFunc import *
from .apiLib import *
Solution 2:[2]
I figured out the issue or rather, found a workaround that stopped the error.
In app.py:
import sys
sys.path.append(".")
I also changed my folder structure around to:
app.py
mainFunctions
-----__init__.py
------main.py
apiFunctions
-----__init__.py
-----apiFunc.py
apiLib
-----__init__.py
-----api.py
Once I did that, it ended up working. I dont think by any means this is the ideal solution, but it stopped the errors.
The ideal solution would be to have sub folders within the apiFunctions folder that can be called in mainFunctions without the need to use sys.path.append('.').
I think the project keeps getting confused between absolute vs relative imports as functions are being called from nested libraries which messes up the path structure.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ray |
