2

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 Screenshot](https://imgur.com/a/1yQnQZF)[![enter image description here]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 Project Structure screenshot

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:

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'

I am running using

python manage.py runserver
Ed Danileyko
  • 189
  • 2
  • 13
  • Are you using `python app/manage.py runserver` to run your server? Or are you getting these errors from a python shell? – A. J. Parr Jul 18 '19 at 23:18
  • ```python manage.py runserver``` also ```docker-compose up``` both give me the same errors – Ed Danileyko Jul 19 '19 at 00:30
  • 2
    Have you tried `from core.models import Tag`? That's how I usually import my Django apps models. I was also just reading [this SO Q/A](https://stackoverflow.com/a/47030746/784648) about the initial `ValueError` which might give more insight. – A. J. Parr Jul 19 '19 at 00:58
  • .....yes this did the trick. I'm so confused now. All the imports where I've done this are registering as incorrect in PyCharm, which makes sense to me, but they are in fact, correct... my brain has broken. – Ed Danileyko Jul 19 '19 at 01:43
  • 1
    I've updated my answer in retrospect. Regarding pycharm, I think if you go to settings > `Project: project_name` > `Project Structure`, and change the "Source Folders" to the top level `app` folder instead of (I presume here) the project folder, it should provide the correct imports. – A. J. Parr Jul 19 '19 at 01:50

1 Answers1

2

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 doesn't record where a package was loaded from. So when you do python -m test_A.test, it basically just discards the knowledge that test_A.test is actually stored in package ...

And recommended using from core.models import Tag and it seems to work.

A. J. Parr
  • 7,731
  • 2
  • 31
  • 46