简体   繁体   English

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

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

My employer has a dedicated module 1 we use for internal unit / system test;我的雇主有一个专门的模块1 ,我们用于内部单元/系统测试; however, the author of this module no longer works here and I have been asked to test some devices with it.然而,这个模块的作者不再在这里工作,我被要求用它测试一些设备。

The problem is that pyfoo requires an ancient version of twisted (v8.2.0) and it imports twisted in 33 different files.问题是pyfoo需要一个古老版本的twisted (v8.2.0) 并且它在 33 个不同的文件中导入twisted I tried running pyfoo 's unit tests under v11.0.0 and I don't even see TCP SYN packets 2 .我尝试在 v11.0.0 下运行pyfoo的单元测试,但我什至没有看到 TCP SYN packets 2 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.不幸的是,我已经在我的实验室 linux 服务器上安装了twisted v11.0.0 ,并且我有自己的代码依赖于它。

To solve this problem, I have only come up with the following options:为了解决这个问题,我只提出了以下选项:

Option A .选项A。 Install a new version of python, install virtualenv , and then install an old version of twisted under the virtualenv .安装新版本的python,安装virtualenv ,然后在virtualenv下安装老版本的twisted Only run the tests requiring pyfoo under this new version of python.仅在 python 的这个新版本下运行需要pyfoo的测试。

Option B .选项B。 Edit all 33 of the files with the following: DIR = '../'; sys.path.insert(0, DIR)使用以下内容编辑所有 33 个文件: DIR = '../'; sys.path.insert(0, DIR) DIR = '../'; sys.path.insert(0, DIR) and install the old version of python in the appropriate directory below the source. DIR = '../'; sys.path.insert(0, DIR)并将旧版本 python 安装在源码下的相应目录中。

Option C .选项 C Attempt to fix pyfoo to use v11.0.0 3尝试修复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 pyfoo for sake of argument为了争论起见,我们称它为pyfoo
  2. The unit tests connect to one of our local lab servers and exercises basic te.net functionality单元测试连接到我们的本地实验室服务器之一并练习基本的 te.net 功能
  3. This option is almost a non-starter... pyfoo is not trivial, and I have a short deadline for this work.这个选项几乎是一个非启动器... pyfoo不是微不足道的,我的这项工作的截止日期很短。

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.require gets called;但是,如果在调用pkg_resources.require之前导入了 twisted ,则这将不起作用(选项 B 的任何其他变体也不会); twisted will already be in sys.modules twisted将已经在sys.modules

OP Edit : Minor syntax correction, per pkg_resources docs OP 编辑:根据pkg_resources文档进行少量语法更正

If SingleNegationElimination's solution doesn't work, be aware that you don't need to replace all 33 instances of the import;如果 SingleNegationElimination 的解决方案不起作用,请注意您不需要替换导入的所有 33 个实例; you only need to modify sys.path at the entry points;您只需要在入口点修改sys.path eg you could target just your module's __init__.py files.例如,您可以仅针对模块的__init__.py文件。

There you would insert eg在那里你会插入例如

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

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

It took me a bit of trial and error to fix my situation;我花了一些试验和错误来解决我的情况。 which involved the accepted answer and it's additional comments (mentioning adding _ requires _)其中涉及已接受的答案及其附加评论(提到添加 _需要_)

__requires__= 'twisted==8.2.0'
import pkg_resources
pkg_resources.require("twisted==8.2.0")
import twisted  

    

You should do uninstall and install before import.您应该在导入前卸载并安装。

First,第一的,

!pip uninstall igraph -y
!pip uninstall python-igraph -y
!pip install python-igraph==0.9.6
!pip install cairocffi

Then,然后,

import igraph
print(igraph.__version__)
% 0.9.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM