简体   繁体   中英

Importing .py file of outer module from inner module

The structure is:

hello/
    __init__.py
    params.py
    bye/
        __init__.py
        params2.py

I want to call constant A that lives in params from params2 file...

I tried:

from ..hello.params import A

But I get the following error:

ValueError: Attempted relative import in non-package

hello is not a package? Thanks in advance!!!

Your code is not working because you are trying to import something that is one level up relative to the directory hello

To make it work you need to be aware what is your PYTHONPATH. If there is hello/bye in PYTHONPATH, this may work:

from ..params import A

It might be also the case that there is your project root hello in PYTHONPATH, then you may just do

from params import A

All depends on how you have installed your package or which IDE config you are currently using or which path you have explicitely added to PYTHONPATH

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