强制 python 使用旧版本的模块(比我现在安装的模块)
我的雇主有一个专用模块1,用于内部单元/系统测试;然而,该模块的作者不再在这里工作,我被要求用它来测试一些设备。
问题是 pyfoo
需要旧版本的 twisted
(v8.2.0),并且它在 33 个不同的文件中导入 twisted
。我尝试在 v11.0.0 下运行 pyfoo
的单元测试,但我什至没有看到 TCP SYN 数据包2。不幸的是,我已经在我的实验室 Linux 服务器上安装了 twisted v11.0.0 并且我有自己的代码,具体取决于在它上面。
为了解决这个问题,我只提出了以下选项:
选项A。安装新版本的python,安装virtualenv
,然后在virtualenv
下安装旧版本的twisted
。仅在此新版本的 python 下运行需要 pyfoo
的测试。
选项B。使用以下内容编辑所有 33 个文件:DIR = '../'; sys.path.insert(0, DIR)
并将旧版本的 python 安装在源代码下面的相应目录中。
选项C。尝试修复 pyfoo
以使用 v11.0.03
我缺少任何选项吗?除了上面的选项A之外,还有更优雅的方法来解决这个问题吗?
END-NOTES:
- 为了便于讨论,我们将其称为
pyfoo
- 单元测试连接到我们的本地实验室服务器之一并练习基本的 telnet 功能
- 这个选项几乎是不可能的... pyfoo 并不是一件小事,而且我这项工作的截止日期很短。
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:
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
选项 B. 的更好版本是替换
为
,只要安装了正确版本的twisted,就会安排导入它,否则会引发异常。这是一个更便携的解决方案。
不过,如果在调用
pkg_resources.require
之前导入 twins,那么这将不起作用(选项 B 的任何其他变体也不会起作用);twisted
已经在sys.modules
中OP 编辑:根据
pkg_resources
文档A better version of option B. would be to replace
by
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 insys.modules
OP Edit: Minor syntax correction, per
pkg_resources
docs如果 SingleNegationElimination 的解决方案不起作用,请注意您不需要替换导入的所有 33 个实例;你只需要在入口点修改
sys.path
;例如,您可以仅针对模块的__init__.py
文件。在那里你可以插入例如
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.
我经历了一些尝试和错误才解决了我的情况;其中涉及已接受的答案及其附加注释(提到添加
__requires__
):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__
):我无法告诉你什么是最适合你的情况,但你也许可以考虑:
选项 D:在虚拟机中运行它(例如使用 Windows 7)
选项 E:在另一台计算机上安装旧版本的 python/twisted
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)
Option E: install old version of python/twisted on another machine
您应该在导入之前卸载并安装。
首先,
然后,
You should do uninstall and install before import.
First,
Then,