简体   繁体   English

尽管PyCharm具有自动填充功能,但ValueError尝试了相对导入而不是顶级软件包

[英]ValueError attempted relative import beyond top-level package, despite PyCharm autofilling suggestions

I am attempting to import a model from a sibling package and am getting 我正在尝试从同级包中导入模型并得到

ValueError: attempted relative import beyond top-level package

Strangely, I am auto-filling based on PyCharm suggestions, so the IDE is registering the module, but my build is failing... 奇怪的是,我是根据PyCharm建议自动填充的,所以IDE正在注册模块,但是我的构建失败了...

PyCharm屏幕截图](https://imgur.com/a/1yQnQZF)[![在此处输入图片描述 ] 1 ] 1

Here is my project structure: 这是我的项目结构:

app
 \
  +-core
  |  \
  |   +- __init__.py
  |   +- models.py   <- the Tag model is present here
  |
  +-scheduler
  |  \
  |   +- __init__.py
  |   +- serializers.py  <- importing app.core.models.Tag in this file
  |
  +- __init__.py

PyCharm项目结构屏幕截图

app.scheduler.serializers.py: app.scheduler.serializers.py:

from rest_framework import serializers
from ..core.models import Tag


class TagSerializer(serializers.ModelSerializer):
    """Serializer for tag objects"""

    class Meta:
        model = Tag
        fields = ('id', 'name')
        read_only_fields = ('id',)

I've been scratching my head about this and can't seem to figure it out... 我一直在为此挠头,似乎无法弄清楚……

I've tried using an absolute path, even adding it using the PyCharm import utility: 我尝试使用绝对路径,甚至使用PyCharm导入实用程序将其添加:

from rest_framework import serializers
from app.core.models import Tag


class TagSerializer(serializers.ModelSerializer):
    """Serializer for tag objects"""

    class Meta:
        model = Tag
        fields = ('id', 'name')
        read_only_fields = ('id',)

but then I get: ModuleNotFoundError: No module named 'app.core' 但随后我得到: ModuleNotFoundError: No module named 'app.core'

I am running using 我正在使用

python manage.py runserver

The real answer was that the top level app folder is not included in the python path, I referred to this stack overflow answer regarding how: 真正的答案是顶层应用程序文件夹未包含在python路径中,我提到堆栈溢出答案涉及以下方面:

... python doesn't record where a package was loaded from. ... python不会记录软件包从何处加载。 So when you do python -m test_A.test, it basically just discards the knowledge that test_A.test is actually stored in package ... 因此,当您执行python -m test_A.test时,它基本上只是舍弃了test_A.test实际上存储在包中的知识...

And recommended using from core.models import Tag and it seems to work. 并建议使用from core.models import Tag ,它似乎可以工作。

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

相关问题 ValueError:尝试在顶级包python之外进行相对导入 - ValueError: attempted relative import beyond top-level package python ValueError:尝试相对导入超出顶级包(Scrapy) - ValueError: attempted relative import beyond top-level package (Scrapy) ValueError:尝试运行包时尝试相对导入超出顶级包 - ValueError: attempted relative import beyond top-level package while try to run package ValueError:尝试相对导入超出顶级 package - 导入文件时 - ValueError: attempted relative import beyond top-level package - when import file 仅在PyCharm中尝试了相对顶级包消息以外的相对导入 - attempted relative import beyond top-level package message in PyCharm only 如何解决“ ValueError:尝试相对顶级包进行相对导入” - How to resolve “ValueError: attempted relative import beyond top-level package” 从celery导入current_app给出ValueError:尝试了相对顶级包之外的相对导入 - importing current_app from celery is gives ValueError: attempted relative import beyond top-level package 无法解决“ ValueError:尝试相对顶级包进行相对导入” - Cannot Solve “ValueError: attempted relative import beyond top-level package” ImportEror:尝试相对导入超出顶级包 - ImportEror: attempted relative import beyond top-level package 值错误:尝试相对导入超出顶级 package - Value Error: Attempted relative import beyond top-level package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM