强制python使用较旧版本的模块(比我现在安装的版本) [英] Force python to use an older version of module (than what I have installed now)

查看:657
本文介绍了强制python使用较旧版本的模块(比我现在安装的版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的雇主有一个专用模块 1 ,用于内部单元/系统测试;但是,该模块的作者不再在这里工作,并且要求我使用它来测试一些设备.

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.

问题在于pyfoo需要twisted的较旧版本(v8.2.0),并且它会在33个不同的文件中导入twisted.我尝试在v11.0.0下运行pyfoo的单元测试,但我什至看不到TCP SYN数据包 2 .不幸的是,我已经在实验室linux服务器上安装了 twisted v11.0.0 ,并且我有自己的代码,具体取决于在上面.

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.

我一直在想办法解决这个问题,但是我只能提出以下选择:

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

选项A .安装新版本的python,安装virtualenv,然后在virtualenv下安装旧版本的twisted.仅在此新版本的python下运行需要pyfoo的测试.

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.

选项B .使用以下命令编辑所有33个文件:DIR = '../'; sys.path.insert(0, DIR)并将旧版本的python安装在源代码下面的相应目录中.

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.

选项C .尝试修复pyfoo以使用v11.0.0 3

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

我有没有其他选择?除了上面的选项A,还有没有更优雅的方法来解决此问题?

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


END-NOTES:

  1. 为了争辩而将其称为pyfoo
  2. 单元测试连接到我们的本地实验室服务器之一,并练习基本的telnet功能
  3. 这个选项几乎是入门级的... pyfoo并非微不足道,而且我的工作期限很短.
  1. Let's call it pyfoo for sake of argument
  2. The unit tests connect to one of our local lab servers and exercises basic telnet functionality
  3. This option is almost a non-starter... pyfoo is not trivial, and I have a short deadline for this work.

推荐答案

选项B的一个更好的版本是替换

A better version of option B. would be to replace

import twisted

作者

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

,只要已安装,它将安排正确版本的twist导入,否则会引发异常.这是一种更可移植的解决方案.

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.

但是,如果在pkg_resources.require调用之前引入了twisted,这将不起作用(选项B的任何其他变量也不会). twisted将已经在sys.modules

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

OP Edit :根据 pkg_resources文档

OP Edit: Minor syntax correction, per pkg_resources docs

这篇关于强制python使用较旧版本的模块(比我现在安装的版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆