简体   繁体   中英

ValueError: Attempted relative import in non-package - relative import in python

The folder directory is:

server/
    |_entities.py
    |_config.py

In my entities.py file, I have the following:

from .config import config

But when I debug, it results in the following problem:

File "entities.py", line 14, in <module>
from .config import config
ValueError: Attempted relative import in non-package

Make sure you have an __init__.py in the server directory. This is an empty file required to make Python treat directories containing the file as packages.

Then try from server.config import config

Server is the top level name of your package.

From the docs

Packages are a way of structuring Python's module namespace by using “dotted module names”. For example, the module name AB designates a submodule named B in a package named A

You should also consider renaming the variable config to something else so that it's not confused with the file called config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM