Linux 强制 python 使用旧版本的模块(比我现在安装的版本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6445167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:40:15  来源:igfitidea点击:

Force python to use an older version of module (than what I have installed now)

pythonlinuxtwistedpython-import

提问by Mike Pennington

My employer has a dedicated module1we 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 pyfoorequires an ancient version of twisted(v8.2.0) and it imports twistedin 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.0installed on my lab linux server and I have my own code that depends on it.

问题是它pyfoo需要一个古老的twisted(v8.2.0)版本,它导入twisted了 33 个不同的文件。我尝试pyfoo在 v11.0.0 下运行单元测试,我什至没有看到 TCP SYN 数据包2。不幸的是,我已经在我的实验室 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 twistedunder the virtualenv. Only run the tests requiring pyfoounder this new version of python.

选项A。安装新版本的python,安装virtualenv,然后twistedvirtualenv. 只运行pyfoo在这个新版本的 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.

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

Option C. Attempt to fix pyfooto use v11.0.03

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

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 pyfoofor 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... pyfoois not trivial, and I have a short deadline for this work.
  1. pyfoo为了争论,让我们称之为
  2. 单元测试连接到我们的本地实验室服务器之一并练习基本的 telnet 功能
  3. 这个选项几乎pyfoo是不可能的……不是微不足道的,而且我有一个很短的期限来完成这项工作。

采纳答案by SingleNegationElimination

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.

这将安排导入正确版本的扭曲,只要它已安装,否则引发异常。这是一个更便携的解决方案。

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

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

OP Edit: Minor syntax correction, per pkg_resourcesdocs

OP 编辑:根据文档进行少量语法更正pkg_resources

回答by Gerrat

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

回答by jmetz

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.pathat the entry points; e.g. you could target just your module's __init__.pyfiles.

如果 SingleNegationElimination 的解决方案不起作用,请注意您不需要替换导入的所有 33 个实例;您只需要sys.path在入口点进行修改;例如,您可以只针对您的模块__init__.py文件。

There you would insert e.g.

在那里你会插入例如

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