Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.88k stars 1.2k forks source link

Attempted relative import with no known parent package when deployed to Amazon Web Services #1426

Open kyeljmd opened 6 years ago

kyeljmd commented 6 years ago

I am developing a flask app locally, and I am using relative imports for my app. Here's my directory structure.

here's my app.py

from flask import Flask
from flask import request

from .dataproviders import provider
from .usecase import useCases

import json
import psycopg2

import os

app = Flask(__name__)
conn = None
cognito_data_provider = None
admin_usecase = None
##Initialize Services and dependencies
try:
    conn = psycopg2.connect(
        database=os.getenv('DATABASE_NAME'),
        user=os.getenv('DATABASE_USER'),
        password=os.getenv('DATABASE_PASSWORD'),
        host=os.getenv('DATABASE_HOST'),
        port=os.getenv('DATABASE_PORT'))

    cognito_data_provider = provider.CognitoDataProvider()
    admin_usecase = useCases.AdminUseCase(conn, cognito_data_provider)
except Exception as e:
    print(">>>> Error encountered setting up application depnedencies")

@app.route("/")
def hello():
    return admin_usecase.mes()

And here's my directory structure.

Directory Structure

Everything is running welll locally, however upon deploying it to AWS using zappa. produces this cloudwatch error.

attempted relative import with no known parent package: ImportError Traceback (most recent call last): File "/var/task/handler.py", line 509, in lambda_handler return LambdaHandler.lambda_handler(event, context) File "/var/task/handler.py", line 237, in lambda_handler handler = cls() File "/var/task/handler.py", line 129, in __init__ self.app_module = importlib.import_module(self.settings.APP_MODULE) File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "/var/task/app.py", line 4, in <module> from .dataproviders import provider ImportError: attempted relative import with no known parent package

I am currently working on virtualenvwrapper in windows

ablakey commented 6 years ago

Does the usecase folder have an __init__.py ?

kyeljmd commented 6 years ago

@ablakey Yes, all of the subfolders have __init__.py

[Edit: fixed markdown blunder -@scoates]

shreyashag commented 6 years ago

Any leads on this? I am facing the same problem in one of my projects

yunxingwang commented 6 years ago

facing same issue in my project after deployed on AWS. note that the zipped file is bigger than 60M

rubelw commented 6 years ago

Same issue as above. Unable to import module 'handler': attempted relative import with no known parent package

aditya985 commented 6 years ago

try installing flask and all other dependencies using pip in your virtual environment where you are running zappa

rlapar commented 6 years ago

Try renaming your virtualenv, it solved the issue for me. The problem was, that the name of virtualenv and the name of the app were the same.

gregorypierce commented 5 years ago

See this a few times. This is almost certainly because zappa or flask (likely zappa) is not in your virtualenv.

pip freeze > requirements.txt

Make sure all of the dependencies you expect are showing up in the requirements.txt.

catsclaw commented 5 years ago

Flask and zappa are both installed in my virtualenv, and the name of my virtualenv is different than my app. Still get this error.

jtanios commented 5 years ago

I'm having a similar issue. Any leads on this would be great.

jetsbee commented 5 years ago

At the top of your file, you can put this code.

__name__ = '.'.join(__name__.split('/'))
__package__ = '.'.join('.'.join(__name__.split('/')).split('.')[:-1])
MexsonFernandes commented 5 years ago

Use

Flask==1.0.3 Flask-Mail==0.9.1 Flask-WTF==0.14.2 zappa==0.48.2

The above versions solved my problem.

probably-coding commented 4 years ago

I got the same error. "Unable to import module 'handler': attempted relative import with no known parent package" Any assistance would be very much appreciated

Miguelme commented 4 years ago

It's happening for me too.