Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError - attempted relative import with no known parent package

Tags:

python

docker

It looks like to be a known problem and I am not the only one who encounter this issue. But none of the StackOverflow topics I've read helped me.

So here is the tree of my folder:

.
├── Dockerfile
├── app
│   ├── __init__.py
│   ├── app.py
│   ├── config.py
│   ├── controllers
│   │   └── home.py
│   ├── models.py
│   └── views
│       └── home.py
├── database.conf
├── docker-compose.yml
├── jarvis.conf
└── requirements.txt

As you can see I've tried to dockerized my app.

Let's have a look to my Dockerfile and docker-compose.yml

Dockerfile:

FROM python:3.6.8-alpine

LABEL maintainer="Jordane * <*>"
LABEL version="1.0.0"

RUN apk add build-base postgresql-dev

RUN pip install --upgrade pip
COPY requirements.txt /
RUN pip install -r requirements.txt

COPY app/ /app
WORKDIR /app

CMD ["gunicorn", "-w 1", "app:app", "-b", "0.0.0.0:3000"]

docker-compose.yml:

version: '3.5'

services:

  db:
    container_name: postgres
    image: postgres:11.2-alpine
    env_file: database.conf
    ports:
      - 5432:5432
    volumes:
      - dbdata:/var/lib/postgresql/data

  web:
    build: .
    container_name: flask
    restart: always
    env_file: 
      - jarvis.conf
      - database.conf
    environment:
      - PYTHONDONTWRITEBYTECODE=1
    ports:
      - 6876:3000
    volumes:
      - ./app/:/app
    depends_on:
      - db

volumes:
  dbdata:

Here is the begin of my trouble I think

I've wrote this init.py:

from flask import Flask
import flask_sqlalchemy

from .models import db
from . import config

def create_app():
    flask_app = Flask(__name__)
    flask_app.config['SQLALCHEMY_DATABASE_URI']= config.DB_CONN_URI
    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    flask_app.app_context().push()
    db.init_app(flask_app)
    db.create_all()
    return flask_app

and as you saw above in my Dockerfile I am running my app with gunicorn and run the app.

app.py:

""" Jarvis slackBot v1.0 (api) """

__author__ = "titus"

from flask import request, jsonify

from . import create_app
from .models import User, db

from views.home import home_bp

from loguru import logger

app = create_app()
# logger.add("app.log", rotation="500 MB")

app.register_blueprint(home_bp, url_prefix='/home')

And here is the error :

flask  |     from . import create_app
flask  | ImportError: attempted relative import with no known parent package

I've followed this Tutorial to help me: https://medium.com/@hmajid2301/implementing-sqlalchemy-with-docker-cb223a8296de

So it's supposed to work ...

If I replace :

  • from . import create_app by from __init__ import create_app
  • from .models import User, db by from models import User, db
  • from .models import db by from models import db
  • from . import config by import config

It works better, but I really feel like I am doing something wrong.

like image 643
jgengo Avatar asked Mar 07 '19 10:03

jgengo


2 Answers

I'm relatively new to python, but ran into this issue today with cloudscraper.

The code originally was:

from . import __version__ as cloudscraper_version

I installed cloudscraper using pip3, so it installed directly to C:\Python39\Lib\site-packages\cloudscraper I received the same error message when trying to run one of the py files.

I couldn't really find anything that wouldn't be more headache than it was worth (moving, renaming files, etc) as I was just wanting to run the help.py in cloudscraper. I have a ton of modules and packages and finally got them all to where they interact with my interpreter like I want, so I didn't want to move anything. I fixed it by doing this:

from cloudscraper import __version__ as cloudscraper_version

*Note, if you used 'run as administrator' to install the package via cmd, but utilize the files through a user profile on your pc, you'll need to change permissions giving access to your pc user profile on the particular py file you're wanting to edit. (Right click file>Properties>Security>'edit permissions'>Full Control)

Just wanted to share in case this could help someone else that might run into this issue.

like image 191
canelane5517 Avatar answered Sep 20 '22 18:09

canelane5517


This error is due to the current version of gunicorn (19.9.0) using __import__(your_app) to load your app which apparently doesn't import parent packages. This means that your __init__.py is never called.
(See https://github.com/benoitc/gunicorn/blob/19.x/gunicorn/util.py#L350)

This seems to be fixed in the current repo version of gunicorn, which I think will be released with 20.0.0.
(See https://github.com/benoitc/gunicorn/blob/master/gunicorn/util.py#L331)

The easiest workaround is to use:
CMD ["gunicorn", "-w 1", "app", "-b", "0.0.0.0:3000"]
and adding this to (to the bottom of) your __init__.py:
from .app import app

Or even better, putting create_app in a separate file, and having only imports in your __init__.py. Just make sure create_app is imported before app.

like image 20
Nicola Avatar answered Sep 22 '22 18:09

Nicola