[英]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... 我想从params2文件中调用存在于params中的常量
A
...
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? hello
不是包吗? 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
您的代码无法正常工作,因为您正在尝试导入相对于目录
hello
向上一级的内容
To make it work you need to be aware what is your PYTHONPATH. 要使其工作,您需要知道什么是PYTHONPATH。 If there is
hello/bye
in PYTHONPATH, this may work: 如果在PYTHONPATH中
hello/bye
,则可能有效:
from ..params import A
It might be also the case that there is your project root hello
in PYTHONPATH, then you may just do 也可能是PYTHONPATH中有您的项目根
hello
,那么您可能只是这样做
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 一切都取决于您如何安装软件包,当前使用的IDE配置或明确添加到PYTHONPATH的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.