简体   繁体   English

从内部模块导入外部模块的.py文件

[英]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.

相关问题 从“外部”目录导入模块 [Python] - Importing module from "outer" directory [Python] 从.py文件中获取包根和完整模块名称以进行导入 - Get package root and full module name from .py file for importing 导入 py 文件错误:没有名为“文件名”的模块 - importing py file error: no module named 'filename' Django:从一个模块导入一个视图比在views.py文件中包含它要慢吗? - Django: is importing a view from a module slower than including it in the main views.py file? Python 模块使用前导下划线导入自身,但没有对应的 .py 文件 - Python module importing itself with leading underscore but there is no corresponding .py file 将.py文件作为模块导入时没有显示文档字符串? - Docstring not showing up when importing .py file as a module? 使用 __init__.py 从目录导入本地 python 模块 - Importing local python module from directory with __init__.py 与__init__.py混淆并从子模块导入父模块中的类 - Confusion with __init__.py and importing classes in parent module from submodule Python 从另一个文件夹导入 .py 时找不到模块错误 - Python No module found error while importing .py from another folder 从第二个模块导入模块 - importing module from second module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM