Relative import beyond top-level package Error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Relative import beyond top-level package Error



I have a project named <code>tweetme</code> which has two apps <code>account</code> and <code>tweets</code>
I have a project named tweetme which has two apps account and tweets,i have serializers.py file in both of the apps to serialize data. So when i try to relative import the serializer class from account/api/serializers.py to another app tweets/api/serializers.py, the relative import shows error.


tweetme


account


tweets


serializers.py


account/api/serializers.py


tweets/api/serializers.py



enter image description here



1- 1st i tried full path from src.account.api.serializers import UserDisplaySerializer, it gave error in console that ModuleNotFoundError: No module named 'src'


from src.account.api.serializers import UserDisplaySerializer


ModuleNotFoundError: No module named 'src'



enter image description here



2- Then i tried from ...account.api.serializers import UserDisplaySerializer,its showing error ValueError: attempted relative import beyond top-level package.


from ...account.api.serializers import UserDisplaySerializer


ValueError: attempted relative import beyond top-level package



So what am i doing wrong? How to do relative import properly?



[![User Serializer class in account app][4]][4]


account





Try to add __init__.py in your src folder.
– Mad Lee
10 mins ago




__init__.py


src





@MadLee Sir, still its showing ValueError: attempted relative import beyond top-level package
– Chidananda Nayak
6 mins ago


ValueError: attempted relative import beyond top-level package





How about your full path import? Where do you run your project?
– Mad Lee
36 secs ago


full path import




1 Answer
1



Seems you are missing __init__.py in src folder.

From the python docs


__init__.py


src



The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.


__init__.py


string


__init__.py


__all__



Remove all relative imports used from your project/app and use absolute import, because
Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name