How to solve "attempted relative import beyond top-level package" when importing in Python

Error summary

With the following directory structure

root/
 ├ core/
 │ └ base.py
 │ 
 ├ sub/
 │ └ subA.py

Try to call base.py from subA.py.

sub.py


from ..core.base import *

Then the following error occurs

Exception Value: attempted relative import beyond top-level package

The cause is that there is a specification that "Python uses the current directory of the file to be executed as the root directory of the highest level", and it occurs because it can not be moved to the upper directory.

Solutions

You can set the environment variable PYTHONPATH.

export PYTHONPATH=/root

And modify the code as follows

sub.py


from core.base import *

Now that `` `/ rootis added to the package search path when python is executed, you can search for directories under/root```.

Reference: https://docs.python.org/ja/3/using/cmdline.html#envvar-PYTHONPATH

Recommended Posts

How to solve "attempted relative import beyond top-level package" when importing in Python
attempted relative import beyond top-level package and how to resolve when angry
How to avoid import errors when running python in Synology DSM Task Scheduler
How to develop in Python
How to import Python library set up in EFS to Lambda
A story about how to specify a relative path in python.
How to import a file anywhere you like in Python
13th Offline Real-time How to Solve Writing Problems in Python
[Python] When you want to import and use your own package in the upper directory
[Python] How to do PCA in Python
How to collect images in Python
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to write offline real time Solve E04 problems in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to hide the command prompt when running python in visual studio 2015
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
[Python] How to use import sys sys.argv
How to install python package in local environment as a general user
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
I wanted to solve ABC159 in Python
How to switch python versions in cloud9
How to use __slots__ in Python class
How to write a string when there are multiple lines in python
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
Convert absolute URLs to relative URLs in Python
How to take multiple arguments when doing parallel processing using multiprocessing in python
How to start Python (Flask) when EC2 starts
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to use Python Image Library in python3 series
How to create a JSON file in Python
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to check / extract files in RPM package
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
Difference in object ID due to import in Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
Error when trying to install psycopg2 in Python
How to handle datetime type in python sqlite3