简体   繁体   中英

Import one module of a package from a different one (in the same package)

Assume I have the following structure of a package:

parent/
  package/
    __init__.py
    utils/
      __init__.py
      foo.py
    apps/
      __init__.py
      bar.py

Now, I want to import the module foo from the module bar . What is the correct way to do it? I will welcome comments on the structure of my project :)

So far I only found answers where everything lives in the same directory... I tried something like:

from ..utils import foo

but I get:

Traceback (most recent call last):
  File "./bar.py", line 4, in <module>
    from ..utils import foo
ValueError: Attempted relative import in non-package

我认为,最好设置一些环境变量或扩展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