简体   繁体   English

在函数调用中使用Python 3+导入包?

[英]Python 3+ import package in a function call?

Overtime I have built up a collection of utility functions for various things. 加班时间,我为各种事情建立了一组实用程序功能。

I would like to put them all in package, with a bit more structure than just a single file containing all the functions. 我想将它们全部放入包中,其结构比仅包含所有功能的单个文件要多。

Some of these functions are written assuming certain packages have been imported eg I have several numpy and pandas utility functions that assume something like import numpy as np 其中一些函数是在假定已导入某些软件包的情况下编写的,例如,我有几个numpypandas实用程序函数,它们假定import numpy as np类的东西

Obviously I will not use this hypothetical package like from <pkg> import * but I do not want to hinder performance either. 显然,我不会像from <pkg> import *那样使用这种假设的软件包from <pkg> import *但我也不想妨碍性能。

So if I have a numpy utility function, should I add this to every function 因此,如果我有一个numpy实用程序函数,应该将其添加到每个函数中

# mypkg.np.utils
import sys

def np_util_fn(...):
    if 'np' not in sys.modules: import numpy as np
    # rest of func

or 要么

# mypkg.np.utils
import sys
if 'np' not in sys.modules: import numpy as np    

def np_util_fn(...):
    # rest of func

which is more performant if I use a different part of this package? 如果我使用此程序包的其他部分,哪个性能更高? eg from pkg.other.utils import fn 例如from pkg.other.utils import fn

Ok, let's analyze your issue. 好的,让我们分析一下您的问题。 Assume you have a file module.py : 假设您有一个文件module.py

print("Module got imported")

and a file test.py with: 和一个文件test.py与:

import module
import module

. If you now execute test.py you will get 如果您现在执行test.py您将获得

Module got imported

. Please note that this line is not outputted two times. 请注意,此行不会输出两次。 This means that python already checks whether a module was already imported (before reimporting it). 这意味着python已经检查了模块是否已经导入(在重新导入之前)。 So your check if 'np' not in sys.modules: import numpy as np is not needed. 因此,请检查if 'np' not in sys.modules: import numpy as np是否if 'np' not in sys.modules: import numpy as np不需要if 'np' not in sys.modules: import numpy as np This check only delays things as it may result in a double check. 此检查只会延迟检查,因为这可能会导致再次检查。

In case you want to reimport a module you need reload(module) . 如果要重新导入模块,则需要reload(module) So if you have 所以如果你有

import module
import module
reload(module)

in code.py you will see the line Module got imported two times. code.py您将看到Module got imported两次Module got imported

This means that 这意味着

import numpy as np

is sufficient. 足够了。 There is no need to check whether it already got imported via: 无需检查是否已通过以下方式导入:

if 'np' not in sys.modules: import numpy as np

It depends whether it is advantageous to do import numpy as np at the very beginning of your script or in a function. 这取决于在脚本的开头或函数中将import numpy as np是否有利。 If the function is executed multiple times, it is advantageous to do so only at the very beginning. 如果函数多次执行,则仅在一开始执行该操作是有利的。 Otherwise you are rechecking whether 'np' is not in sys.modules all the time. 否则,您将一直检查sys.modules中是否始终没有'np'。 In contrast if you can argue that your function is not called to often / is not necessarily executed in your program (eg because it depends on user input) then it may be advantageous (seen from the "point vu" of speed) to import this module in a function only. 相反,如果您可以争辩说您的函数未经常调用/不一定在程序中执行(例如,因为它取决于用户输入),则导入该函数可能是有利的(从速度的“点vu”看)仅在功能中使用模块。

I normally don't use any import statements in functions as I always have the feeling that they blow up the function body and thus reduce readability. 我通常在函数中不使用任何import语句,因为我总是觉得它们会使函数体膨胀,从而降低可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM