简体   繁体   English

导入错误 - 在没有已知父包的情况下尝试相对导入

[英]ImportError - attempted relative import with no known parent package

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.但是我读过的 StackOverflow 主题都没有帮助我。

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.如您所见,我已尝试对我的应用程序进行 dockerized。

Let's have a look to my Dockerfile and docker-compose.yml让我们看看我的 Dockerfile 和 docker-compose.yml

Dockerfile: 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: 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:我写了这个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.正如您在上面的Dockerfile 中看到的,我正在使用 gunicorn 运行我的应用程序并运行该应用程序。

app.py:应用程序.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我已经按照这个教程来帮助我: https : //medium.com/@hmajid2301/implementing-sqlalchemy-with-docker-cb223a8296de

So it's supposed to work ...所以它应该工作......

If I replace :如果我更换:

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

It works better, but I really feel like I am doing something wrong.它工作得更好,但我真的觉得我做错了什么。

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.此错误是由于当前版本的 gunicorn (19.9.0) 使用__import__(your_app)加载您的应用程序,而该应用程序显然没有导入父包。 This means that your __init__.py is never called.这意味着您的__init__.py永远不会被调用。
(See https://github.com/benoitc/gunicorn/blob/19.x/gunicorn/util.py#L350 ) (见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.这似乎在当前的gunicorn repo 版本中得到修复,我认为它将与20.0.0 一起发布。
(See https://github.com/benoitc/gunicorn/blob/master/gunicorn/util.py#L331 ) (见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 :并将其添加到(底部)您的__init__.py
from .app import app

Or even better, putting create_app in a separate file, and having only imports in your __init__.py .或者更好的是,将create_app放在一个单独的文件中,并且只在__init__.py 中导入。 Just make sure create_app is imported before app .只需确保在app之前导入create_app

I'm relatively new to python, but ran into this issue today with cloudscraper.我对 python 比较陌生,但今天在 cloudcraper 上遇到了这个问题。

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.我使用 pip3 安装了 cloudcraper,因此它直接安装到 C:\\Python39\\Lib\\site-packages\\cloudscraper 我在尝试运行其中一个 py 文件时收到了相同的错误消息。

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.我真的找不到任何比它更令人头疼的东西(移动、重命名文件等),因为我只是想在 cloudcraper 中运行 help.py。 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. *注意,如果您使用“以管理员身份运行”通过 cmd 安装软件包,但通过您电脑上的用户配置文件使用这些文件,您将需要更改权限以访问您在特定 py 文件上的 PC 用户配置文件想编辑。 (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.只是想分享,以防这可以帮助可能遇到此问题的其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入错误:尝试在没有已知父包的情况下进行相对导入? - ImportError: attempted relative import with no known parent package? ImportError:尝试在没有已知父 package 的情况下进行相对导入:( - ImportError: attempted relative import with no known parent package :( ImportError:尝试相对导入,没有已知的父包 - ImportError: attempted relative import with no known parent package 从 。 导入视图导入错误:尝试在没有已知父包的情况下进行相对导入 - from . import views ImportError: attempted relative import with no known parent package Python3 ImportError:尝试在没有已知父项的情况下进行相对导入 package - Python3 ImportError: attempted relative import with no known parent package Python ImportError:尝试在没有已知父包的情况下进行相对导入 - Python ImportError: attempted relative import with no known parent package Python:“ImportError:在没有已知父包的情况下尝试相对导入” - Python: “ImportError: attempted relative import with no known parent package” 如何修复“ImportError:尝试在没有已知父包的情况下进行相对导入” - How to fix 'ImportError: attempted relative import with no known parent package' 路径错误 ImportError:尝试在没有已知父 package 的情况下进行相对导入 - path error ImportError: attempted relative import with no known parent package Django model,ImportError 尝试在没有已知父 package 的情况下进行相对导入 - Django model, ImportError attempted relative import with no known parent package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM