Yet another Python import question sorry….
I am using Flask and use Heroku in production
Everything is working great on my laptop, but impossible to make it work when I push it to Heroku
Here is my directory hierarchy from the root directory :
__init__.py (empty)
app.py
Procfile
sub1/
----__init__.py (empty)
----sub2/
--------__init__.py (empty)
--------file.py
My app.py
:
from sub1.sub2.file import Myclass
x = Myclass()
My sub1/sub2/file.py
file :
class Myclass():
# Some code
My Procfile
:
web: gunicorn app:app
Everything is working great on my laptop but as soon as i push it to Heroku I get the error :
ModuleNotFoundError: No module named 'sub1.sub2.file'
I have tried relative import with from .sub1.sub2.file import Myclass
but I get the error :
ImportError: attempted relative import with no known parent package
I have also tried to :
import sub1 as sub1
insideapp.py
x = sub1.sub2.file.Myclass()
insideapp.py
- add
from . import sub2
insidesub1/__init__.py
- add
from . import file
insidesub1/sub2/__init__.py
- but still not working
I have also tried to add the folders in the sys.path :
base_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(base_dir)
sys.path.append(os.path.join(base_dir, 'sub1'))
sys.path.append(os.path.join(base_dir, 'sub1', 'sub2'))
from sub1.sub2.file import Myclass
Once again, each solution works on my laptop when I do python app.py
or foreman start -f Procfile
but never works on Heroku… is it even possible ?