Sal*_*Sal 3 python python-import
我的 Python 包的一些功能依赖于外部库。这是一个非 Python 包,可能难以安装,所以我希望用户仍然能够使用我的包,但在使用依赖于这个非 Python 包的任何函数时它会失败。
这样做的标准做法是什么?我只能在使用它的方法中导入非 Python 包,但我真的很讨厌这样做
我目前的设置:
myInterface.py
myPackage/
--classA.py
--classB.py
Run Code Online (Sandbox Code Playgroud)
这些接口脚本myInterface.py进口classA和classB与classB进口非Python包。如果导入失败,我会打印警告。如果myMethod被调用并且没有安装包,下游会出现一些错误,但我不会在任何地方捕获它,也不会警告用户。
classB每次调用接口脚本时都会导入,所以我不会有任何失败,这就是为什么我包含pass. 就像我上面说的,我可以在方法内部导入并让它在那里失败,但我真的很喜欢将所有导入保存在一个地方。
来自 classB.py
try:
import someWeirdPackage
except ImportError:
print("Cannot import someWeirdPackage")
pass
class ClassB():
...
def myMethod():
swp = someWeirdPackage()
...
Run Code Online (Sandbox Code Playgroud)
If you are only importing one external library, I would go for something along these lines:
try:
import weirdModule
available = True
except ImportError:
available = False
def func_requiring_weirdmodule():
if not available:
raise ImportError('weirdModule not available')
...
Run Code Online (Sandbox Code Playgroud)
The conditional and error checking is only needed if you want to give more descriptive errors. If not you can omit it and let python throw the corresponding error when trying to calling a non-imported module, as you do in your current setup.
If multiple functions do use weirdModule, you can wrap the checking into a function:
def require_weird_module():
if not available:
raise ImportError('weirdModule not available')
def f1():
require_weird_module()
...
def f2():
require_weird_module()
...
Run Code Online (Sandbox Code Playgroud)
On the other hand, if you have multiple libraries to be imported by different functions, you can load them dynamically. Although it doesn't look pretty, python caches them and there is nothing wrong with it. I would use importlib
import importlib
def func_requiring_weirdmodule():
weirdModule = importlib.import_module('weirdModule')
Run Code Online (Sandbox Code Playgroud)
Again, if multiple of your functions import complicated external modules you can wrap them into:
def import_external(name):
return importlib.import_module(name)
def f1():
weird1 = import_external('weirdModule1')
def f2():
weird2 = import_external('weirdModule2')
Run Code Online (Sandbox Code Playgroud)
And last, you could create a handler to prevent importing the same module twice, something along the lines of:
class Importer(object):
__loaded__ = {}
@staticmethod
def import_external(name):
if name in Importer.__loaded__:
return Importer.__loaded__[name]
mod = importlib.import_module(name)
Importer.__loaded__[name] = mod
return mod
def f1():
weird = Importer.import_external('weird1')
def f2():
weird = Importer.import_external('weird1')
Run Code Online (Sandbox Code Playgroud)
Although I'm pretty sure that importlib does caching behing the scenes and you don't really need for manual caching.
In short, although it does look ugly, there is nothing wrong with importing modules dynamically in python. In fact, a lot of libraries rely on this. On the other hand, if it is just for an special case of 3 methods accessing 1 external function, do use your approach or my first one in case you cant to add custom sception handling.
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |