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

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.

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:

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.

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.

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

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

END-NOTES:

Let's call it pyfoo for sake of argument

The unit tests connect to one of our local lab servers and exercises basic telnet functionality

This option is almost a non-starter... pyfoo is not trivial, and I have a short deadline for this work.

解决方案

A better version of option B. would be to replace

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; twisted will already be in sys.modules

OP Edit: Minor syntax correction, per pkg_resources docs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值