gpt4 book ai didi

python - 如何更直接地引用函数?

转载 作者:行者123 更新时间:2023-12-03 03:04:16 25 4
gpt4 key购买 nike

列出目录分析结构如下:

tree analyse
analyse
├── __init__.py
└── vix.py

__init__.py 是空白的,vix.py 包含一个函数 draw_vix

import analyse
analyse.vix.draw_vix()

draw_vix analysis.vix 引用。,我想用 vix 而不是 analysis.vix 引用它code>,即在import analysis后在命名空间中添加vix
编辑__init__.py:

import analyse.vix as vix
from analyse import vix

检查一下:

import analyse
vix.draw_vix()

错误消息:

NameError: name 'vix' is not defined

@hl037_,如果我在__init__.py中添加from .vix import vix

import analyse
vix.draw_vix()

它提出了其他问题:ImportError:无法导入名称“vix”。我想要的是在 __init__.py 中写一些东西,或者在 vix.py 中写一些东西,以使 vix.draw_vix() 采取效果。
如何解决?

最佳答案

你就是不喜欢这个。

import analyse

只会在您的全局命名空间中进行分析。充其量,您可以这样做:

from analyse import vix

将 vix 放入全局变量中。

(或使用星号)

奖励:如果您不想安装分析(即使使用 venv,建议在 venv 中实际安装您的应用程序/模块),在 __init__ 中导入 vix .py,您还可以在其中进行相对导入:

from . import vix

https://docs.python.org/3/reference/import.html

[编辑]一些示例:

文件系统:

analyse
├─ __init__.py
├─ vix.py
├─ viz.py # another submodule to demonstrate some other points
└─ vit.py # yet another submodule to demonstrate some other points

analysis/__init__.py 包含:

import .vit as vit
# from . import vit # works too
vix = 42
viy = 43

analysis/vix.py 包含:

def draw_vix():
print('Hello from drax_vix')

analysis/viz.py 包含:

def draw_viz():
print('Hello from drax_viz')

analysis/vit.py 包含:

def draw_vit():
print('Hello from drax_vit')

如果您的软件包已在任何脚本(或 python 解释器)中安装(例如,您创建了 setup.py 并进行了 pip install analysis),或者是 PYTHONPATH env 变量中列出的目录的子目录)在包装之外,会发生这些事情:

1) 使用别名完全导入:

import analyse.vix as vix
vix.draw_vix()
# works, and print print('Hello from drax_vix')
analyse
# Error : analyse not defined since you import only symbols from analyse.vix, not the modules themselves
analyse.vix
# Same as previous one

2)完全导入

import analyse.vix
analyse.vix.draw_vix()
#Works because python guarantees analyse.vix to be a valid expression
analyse
# analyse module
vix.draw_vix()
# Error : vix undefined. import analyse.vix guarantee analyse.vix to be a valid module, but does not add submodule to global namespace
analyse.vix
# is a module, analyse.vix module has been imported too and set as an attribute of analyse.
analyse.viz
# Error : analyse has no attr viz since viz is a non-imported submodule.
analyse.vit.draw_vit
# Works because analyse is indirectly imported with import analyse.vix, and vit is imported as vit inside analyse/__init__.py and thus an attribute of the module.

3) 仅导入分析

import analyse
analyse.vix
# 42 because analyse.vix module is not imported, thus you saw the variable defined vix inside analyse
vix.draw_vix()
# Error : vix undefined, obviously, you never defined it nor imported it
vit.draw_vit()
# Works because vit is imported in __init__.py and set as an attribute of analyse.

4) 从分析导入 vix, viy, viz, vit

from analyse import vix, viz, vit
analyse.vix
# Error : analyse not define since you imported only symbols
vix
# 42 Because you didn't import analyse.vix and there is a variable vix defined in analyse/__init__.py
viy
# 43
viz
# module viz : viz is not declared as an attribute of analyse, but it falls back to the existing analyse.viz module
vit
# module vit : The one imported inside __init__.py (that is the same as analyse.vit)

5)从分析导入viy

from analyse import viy
# Error : analyse/viy.py does not exist

关于python - 如何更直接地引用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com