Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

To solve this problem, I have 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:**
  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.
like image 975
Mike Pennington Avatar asked Jun 22 '11 19:06

Mike Pennington


People also ask

How do I use a specific version of a library in Python?

To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .

How do I import a specific version of a module?

To install a specific version of a module, specify a RequiredVersion of 1.1. 0.0. This installs the specified version side by side with the installed version. Now, you see both version of the module listed when you use Get-DSCResource .


2 Answers

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

like image 106
SingleNegationElimination Avatar answered Sep 24 '22 23:09

SingleNegationElimination


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.

There you would insert e.g.

import sys sys.path.insert(0, DIR) 
like image 22
jmetz Avatar answered Sep 25 '22 23:09

jmetz