在同一过程中使用不同版本的python库 [英] Using different versions of a python library in the same process

查看:334
本文介绍了在同一过程中使用不同版本的python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个正在开发的python库。在开发过程中,我想使用该库的某些部分来测试它的较新版本。即,使用稳定的代码来测试开发代码。

We've got a python library that we're developing. During development, I'd like to use some parts of that library in testing the newer versions of it. That is, use the stable code in order to test the development code. Is there any way of doing this in python?

编辑:具体来说,我们有一个库(LibA),它具有许多有用的东西。另外,我们还有一个使用LibA的测试库,以提供一些测试工具(LibT)。我们想使用LibT来测试LibA,但是由于LibT依赖LibA,因此我们希望它在测试LibT时使用稳定的LibA版本(因为一旦测试通过,我们将改变LibT以使其与较新的LibA配合使用)。因此,在运行单元测试时,LibA-dev测试将使用依赖LibA稳定的LibT代码。

To be more specific, we've got a library (LibA) that has many useful things. Also, we've got a testing library that uses LibA in order to provide some testing facilities (LibT). We want to test LibA using LibT, but because LibT depends on LibA, we'd rather it to use a stable version of LibA, while testing LibT (because we will change LibT to work with newer LibA only once tests pass etc.). So, when running unit-tests, LibA-dev tests will use LibT code that depends on LibA-stable.

我们想到的一个想法是,在不同的过程中使用RPyC调用稳定的代码,但是以气密的方式实现(确保它是很难的)正确死亡等,并允许多个实例在同一台计算机上同时执行,等等。)

One idea we've come up with is calling the stable code using RPyC on a different process, but it's tricky to implement in an air-tight way (making sure it dies properly etc, and allowing multiple instances to execute at the same time on the same computer etc.).

谢谢

推荐答案

如果您使用依赖于libA(稳定)的libT测试 libA-dev,那么您实际上并没有像在生产环境中那样测试libA-dev。真正测试libA-dev的唯一方法是全力以赴,使libT依赖libA-dev。如果这破坏了单元测试,那将是一件好事-它向您显示需要修复的问题。

If you "test" libA-dev using libT which depends on libA (stable), then you are not really testing libA-dev as it would behave in a production environment. The only way to really test libA-dev is to take the full plunge and make libT depend on libA-dev. If this breaks your unit tests then that is a good thing -- it is showing you what needs to be fixed.

如果您没有单元测试,那么这是时候开始制作它们了(首先使用稳定的libA和libT!)。

If you don't have unit tests, then this is the time to start making them (using stable libA and libT first!).

我建议使用版本控制系统(例如bzr,hg,svn,git )。然后您可以创建项目的分支 stable和 devA。

I recommend using a "version control system" (e.g. bzr,hg,svn,git). Then you could make branches of your project, "stable" and "devA".

要在分支devA上工作,首先要运行

To work on branch devA, you would first run

export PYTHONPATH=/path/to/devA

通过确保PYTHONPATH环境变量排除其他分支,可以确保Python只是在使用所需的模块。

By making sure the PYTHONPATH environment variable excludes the other branches, you're assured Python is using just the modules you desire.

当需要合并时来自开发人员->稳定的代码,版本控制软件也将提供一种简便的方法。

When the time comes to merge code from dev --> stable, the version control software will provide an easy way to do that too.

版本控制还可以使您更加勇敢-尝试进行重大更改并不那么令人恐惧。如果无法解决问题,还原将非常容易。在那和PYTHONPATH技巧之间,您始终可以返回到已知的有效代码。

Version control also allows you to be bolder -- trying major changes is not as scary. If things do not work out, reverting is super easy. Between that and the PYTHONPATH trick, you are always able to return to known, working code.

如果您觉得以上内容只是对您不起作用,并且您必须使用libT-which-depends-on-libA来测试libA-dev,然后需要重命名所有模块并修改所有import语句,以使libA-dev和libA清晰分开。例如,如果libA有一个名为moduleA.py的模块,则将其重命名为moduleA_dev.py。

If you feel the above just simply is not going to work for you, and you must use libT-which-depends-on-libA to test libA-dev, then you'll need to rename all the modules and modify all the import statements to make a clear separation between libA-dev and libA. For example, if libA has a module called moduleA.py, then rename it moduleA_dev.py.

命令

rename -n 's/^(.*)\.py/$1_dev.py/' *.py

将添加 _dev所有* .py文件。 (使用 -n标志,重命名命令只会向您显示预期的重命名。删除 -n以进行实际的命名。)

will add "_dev" to all the *.py files. (With the "-n" flag the rename command will only show you the contemplated renaming. Remove the "-n" to actually go through with it.)

还原重命名,运行

rename -n 's/^(.*)_dev\.py/$1.py/' *.py

接下来,您需要在代码中将所有对moduleA的引用更改为moduleA_dev。命令

Next you'll need to change all references to moduleA to moduleA_dev within the code. The command

find /path/to/LibA-dev/ -type f -name '*.py' -exec sed -i 's/moduleA/moduleA_dev/g' {} \;

将更改LibA-dev中的每个* .py文件,更改 moduleA-> moduleA_dev 。

will alter every *.py file in LibA-dev, changing "moduleA" --> "moduleA_dev".

请谨慎使用此命令。这很危险,因为如果您有一个名为moduleAB的变量,那么它将被重命名为moduleA_devB,而您真正想要的可能是moduleAB_dev。

Be careful with this command. It is dangerous, because if you have a variable called moduleAB then it will get renamed moduleA_devB, while what you really wanted might be moduleAB_dev.

还原此更改(取决于以上警告)

To revert this change (subject to the above caveat),

find /path/to/LibA-dev/ -type f -name '*.py' -exec sed -i 's/moduleA_dev/moduleA/g' {} \;

一旦分离了命名空间,就打破了循环依赖。一旦您对libA-dev满意,就可以更改moduleA_dev.py-> moduleA.py,然后
更改代码中对moduleA_dev-> moduleA的所有引用。

Once you separate the namespaces, you've broken the circular dependency. Once you are satisfied your libA-dev is good, you could change moduleA_dev.py --> moduleA.py and changes all references to moduleA_dev --> moduleA in your code.

这篇关于在同一过程中使用不同版本的python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆