英文:
Change a library version at some point when a condition is met
问题
在Python中,是否可以在运行时更改库版本,当满足某个条件时?在我的情况下,我正在使用Google语音识别(STT)。我正在使用speech_v1
,但在beta版本speech_v1p1beta1
中有一些功能,我只能在那个版本中使用。我不希望完全切换到beta版本,但我希望在满足某些条件时使用它。在我需要更改版本之前,我使用了speech_v1
中的一些方法。这些方法在beta版本中也是可用的。我认为重要的是将库版本的更改全局应用,而不是在函数或类中进行更改。我会感激任何想法。谢谢。
更改库版本在运行时
英文:
Is it possible in python to change a library version at runtime when a condition is met. In my case, I am using the google speech-to-text (STT). I am using the speech_v1
but there some functionality that is only available in the beta version speech_v1p1beta1
. I not comfortable changing completely to the beta version completely but I want to use it when some conditions are met. There are some method am using in the speech_v1
before I need to change the version. Those method are also available in the beta version. I think it is important I make the change to the library version globally instead of within a function or a class. I will appreciate any thought. Thanks
Change library version at runtime
答案1
得分: 1
如果已经导入了主版本(speech_v1
),像这样:
import google_speech
你可以这样做:
if condition:
google_speech=__import__("google_speech_v1p1beta1")
也可以切换回来:
if switch_back_condition:
google_speech=__import__("google_speech")
上述代码片段仅在模块上下文中运行时有效。
如果这是要在函数内运行,你必须将google_speech定义为全局变量:
def your_function_name(*your_args, **your_kwargs):
global google_speech
#更多代码,以及最终的条件
重要提示:
在安装时,你必须安装两个版本,必须使用不同的名称进行安装 [speech_v1==>google_speech
; speech_v1p1beta1==>google_speech_v1p1beta1
]。
关于实例和对象的说明:
在库版本更改之前创建的任何对象将保留其创建版本的功能和属性,版本切换仅适用于在版本切换后新创建的对象。
假设speech_v1
定义了一个类如下:
class Example(object):
def func1():
return 3
而speech_v1p1beta1
像这样定义它:
class Example(object):
def func2():
return 5
如果现在创建了该类的一个实例(obj=Example()
)
然后切换版本(参见上文)
这仍然有效:
obj.func1() # 3 [speech_v1p1beta1:AttributeError]
但这将导致AttributeError:
obj.func2() # AttributeError [speech_v1p1beta1==>5]
你将不得不重建对象。
更改实例库版本是不可能的。
英文:
if the main version(speech_v1
) has been imported like this:
import google_speech
You can do this:
if condition:
google_speech=__import__("google_speech_v1p1beta1")
Switching back is also possible:
if switch_back_condition:
google_speech=__import__("google_speech")
The above code piece will only work when running in module context
If this is supposed to run inside a function, you have to define google_speech as a global variable:
def your_function_name(*your_args,**your_kwargs):
global google_speech
#more code, and the condition eventually
IMPORTANT:
When installing, you have to install both versions, you have to install them with different names [speech_v1==>google_speech
;speech_v1p1beta1==>google_speech_v1p1beta1
]
NOTE ON INSTANCES AND OBJECTS:
Any objects created before the library version was changed will keep the features and properties of the version they were created with, the version switch will only apply to objects newly created after the version was switched.
Lets say speech_v1 defines a class like this:
class Example(object):
def func1():
return 3
and speech_v1p1beta1 defines it like this:
class Example(object):
def func2():
return 5
If you created an instance of this class now(obj=Example()
)
And then you switched versions(see above)
This will still work:
obj.func1()#3[speech_v1p1beta1:AttributeError]
But this will lead to an AttributeError:
obj.func2()#AttributeError[speech_v1p1beta1==>5]
You would have to reconstruct the object.
Changing the instance library version is NOT possible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论