强制python使用旧版本的模块(而不是我现在安装的)

时间:2021-03-25 20:58:56

My employer has a dedicated module1 we use for internal unit / system test; however, the author of this module no longer works here and I have been asked to test some devices with it.

我的雇主有一个专用的模块1我们用于内部单元/系统测试;然而,这个模块的作者不在这里工作,我被要求用它测试一些设备。

The problem is that pyfoo requires an ancient version of twisted (v8.2.0) and it imports twisted in 33 different files. I tried running pyfoo's unit tests under v11.0.0 and I don't even see TCP SYN packets2. Unfortunately, I have already got twisted v11.0.0 installed on my lab linux server and I have my own code that depends on it.

问题是pyfoo需要一个古老的twisted (v8.2.0)版本,并且在33个不同的文件中导入twisted。我尝试在v11.0.0下运行pyfoo的单元测试,甚至没有看到TCP SYN packets2。不幸的是,我已经在我的lab linux服务器上安装了twisted v11.0.0,并且我有自己的代码依赖于它。

I have been wracking my brain for a way around this, but I can only come up with the following options:

我一直在绞尽脑汁想办法解决这个问题,但我只能想到以下几个选择:

Option A. Install a new version of python, install virtualenv, and then install an old version of twisted under the virtualenv. Only run the tests requiring pyfoo under this new version of python.

选项a .安装新版本的python,安装virtualenv,然后在virtualenv下安装旧版本的twisted。只在这个新版本的python下运行需要pyfoo的测试。

Option B. Edit all 33 of the files with the following: DIR = '../'; sys.path.insert(0, DIR) and install the old version of python in the appropriate directory below the source.

选项b编辑所有33个文件,如下所示:DIR = '. /';sys.path。插入(0,DIR)并将旧版本的python安装到源代码下面的适当目录中。

Option C. Attempt to fix pyfoo to use v11.0.03

选项c尝试修复pyfoo以使用v11.0.03

Are there any options I am missing? Is there a more elegant way to solve this problem, besides Option A, above?

我还有什么选择没有?除了以上选项a之外,是否还有更优雅的方法来解决这个问题?


END-NOTES:

  1. Let's call it pyfoo for sake of argument
  2. 为了便于讨论,我们称它为pyfoo
  3. The unit tests connect to one of our local lab servers and exercises basic telnet functionality
  4. 单元测试连接到我们本地的一个实验室服务器,并练习基本的telnet功能。
  5. This option is almost a non-starter... pyfoo is not trivial, and I have a short deadline for this work.
  6. 这个选项几乎是不可能的……pyfoo不是平凡的,我对这项工作有一个很短的期限。

3 个解决方案

#1


21  

A better version of option B. would be to replace

b选项更好的版本是替换

import twisted

by

通过

import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted

which will arrange for the correct version of twisted to be imported, so long as it's installed, and raises an exception otherwise. This is a more portable solution.

只要安装了,就会安排导入正确的twisted版本,否则会引发异常。这是一个更便携的解决方案。

This won't work, though (nor would any other variaton of option B), if twisted gets imported before the pkg_resources.require gets called; twisted will already be in sys.modules

但是,如果在pkg_resources之前导入twisted,那么这个方法就不能工作(选项B中的其他变量也不能)。需要被调用;twisted matrix已经在sys.module中了

OP Edit: Minor syntax correction, per pkg_resources docs

OP编辑:小语法更正,每个pkg_resources文档

#2


1  

I can't tell you what is best in your situation, but you might be able to consider:

我不能告诉你在你的情况下什么是最好的,但是你可以考虑:

Option D: run it in a virtual machine (eg. with Windows 7)

选项D:在虚拟机中运行。与Windows 7)

Option E: install old version of python/twisted on another machine

选项E:在另一台机器上安装旧版本的python/twisted

#3


0  

If SingleNegationElimination's solution doesn't work, be aware that you don't need to replace all 33 instances of the import; you only need to modify sys.path at the entry points; e.g. you could target just your module's __init__.py files.

如果SingleNegationElimination的解决方案无效,请注意,您不需要替换导入的所有33个实例;只需要修改sys即可。入口点的路径;你可以只针对你的模块。py文件。

There you would insert e.g.

你可以在这里插入。

import sys
sys.path.insert(0, DIR)

#1


21  

A better version of option B. would be to replace

b选项更好的版本是替换

import twisted

by

通过

import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted

which will arrange for the correct version of twisted to be imported, so long as it's installed, and raises an exception otherwise. This is a more portable solution.

只要安装了,就会安排导入正确的twisted版本,否则会引发异常。这是一个更便携的解决方案。

This won't work, though (nor would any other variaton of option B), if twisted gets imported before the pkg_resources.require gets called; twisted will already be in sys.modules

但是,如果在pkg_resources之前导入twisted,那么这个方法就不能工作(选项B中的其他变量也不能)。需要被调用;twisted matrix已经在sys.module中了

OP Edit: Minor syntax correction, per pkg_resources docs

OP编辑:小语法更正,每个pkg_resources文档

#2


1  

I can't tell you what is best in your situation, but you might be able to consider:

我不能告诉你在你的情况下什么是最好的,但是你可以考虑:

Option D: run it in a virtual machine (eg. with Windows 7)

选项D:在虚拟机中运行。与Windows 7)

Option E: install old version of python/twisted on another machine

选项E:在另一台机器上安装旧版本的python/twisted

#3


0  

If SingleNegationElimination's solution doesn't work, be aware that you don't need to replace all 33 instances of the import; you only need to modify sys.path at the entry points; e.g. you could target just your module's __init__.py files.

如果SingleNegationElimination的解决方案无效,请注意,您不需要替换导入的所有33个实例;只需要修改sys即可。入口点的路径;你可以只针对你的模块。py文件。

There you would insert e.g.

你可以在这里插入。

import sys
sys.path.insert(0, DIR)