运行测试时,Python导入了错误版本的sqlite [英] Python imports wrong version of sqlite when running tests

查看:96
本文介绍了运行测试时,Python导入了错误版本的sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的Jenkins服务器设置Windows从站,但是由于sqlite错误,某些单元测试失败。

I'm trying to set up a Windows slave for our Jenkins server, but some of the unit tests are failing due to a sqlite error.

使用conda并构建了包含rtree的sqlite的自定义版本。然后,使用此自定义sqlite构建我们的应用程序。当我们在Windows服务器上安装应用程序时,将安装正确版本的sqlite:

We're using conda and have built a custom version of sqlite that includes rtree. Then our application is built using this custom sqlite. When we install our app on the windows server, the correct version of sqlite is installed:

$ conda list

sqlite                    3.16.2                    vc9_0  [vc9]  file://[package_server]

运行应用从命令行可以按预期工作。不过,当我们运行测试时,

Running the app from the command line it works as expected. When we run tests though,

$ nosetests test

...

======================================================================
ERROR: test_vectorise_1 (test.system_test_wallet.test_rasters.test_vectorise.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "farm\rasters\vectorise.py", line 189, in run_vectorise
    proc(progressfn)
  File "farm\process.py", line 1278, in __call__
    for feature in lyr:
  File "C:\bin\Miniconda2\envs\farm_system_test_windows\lib\site-packages\osgeo\
ogr.py", line 2876, in next
    feature = self.GetNextFeature()
  File "C:\bin\Miniconda2\envs\farm_system_test_windows\lib\site-packages\osgeo\
ogr.py", line 1460, in GetNextFeature
    return _ogr.Layer_GetNextFeature(self, *args)
RuntimeError: sqlite3_exec(CREATE VIRTUAL TABLE "rtree_GB_TEST_RP100_VE_5m_27700
_geom" USING rtree(id, minx, maxx, miny, maxy)) failed: no such module: rtree

我添加了一些日志记录以显示python可执行路径和sqlite3路径,并且每次都相同,因此这不是virtualenv问题:

I added some logging to show python executable path and sqlite3 path and it is the same every time, so it's not a virtualenv issue:

C:\bin\Miniconda2\envs\farm_system_test_windows\python.exe
C:\bin\Miniconda2\envs\farm_system_test_windows\lib\sqlite3\__init__.pyc

尽管打印sqlite版本显示出一些差异:

Printing the sqlite version though, shows some differences:

print('sql ver', sqlite3.sqlite_version_info)

从命令行运行应用程序,您将获得(3,16,2),这是正确的版本。运行鼻子测试,您会得到(3,8,11)-一个较旧的版本,可能是找不到rtree的原因。

Running the app from the command line, you get (3, 16, 2), which is the correct version. Running nosetests, you get (3, 8, 11) - an older version and presumably the reason for rtree not being found.

奇怪的是,如果您使用以下命令调用测试文件

Weirdly, if you call a test file with

python path/to/test.py

并让unittest进行处理,最初会导入版本(3、16、2),但是随后运行测试时,您会得到(3、8、11)和测试错误。

and let unittest do its stuff, version (3, 16, 2) is initially imported, but then when the test runs, you get (3, 8, 11) and the test errors.

这是怎么回事?为什么我根据应用程序的调用方式获得不同版本的sqlite?

What is going on here? Why do I get a different version of sqlite depending on how I call the application?

我在Windows Server 2012 R2上使用的是python 2.7.14,conda 4.4.11。

I'm using python 2.7.14, conda 4.4.11 on Windows Server 2012 R2.

更新:
我仍​​然在Jenkins上以及现在在我的开发机上遇到此错误。这不仅仅是一个鼻子问题,当我使用unittest运行测试时,我得到了错误。在两种情况下,PYTHONPATH和sys.path都是相同的,并且调用堆栈是相同的,除了unittest之外,顶部还有另外两个框架。

UPDATE: I'm still getting this error on Jenkins and now on my dev machine too. It's not just a nose issue, as when I run the test using unittest, I get the error. The PYTHONPATH and sys.path are the same in both cases, and the call stack is identical, except with unittest, there are an extra two frames at the top.

File "C:\ProgramData\Anaconda2\envs\farm\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\ProgramData\Anaconda2\envs\farm\lib\runpy.py", line 72, in _run_code
exec code in run_globals

有趣的是,其余的调用堆栈显示main.py的绝对路径,入口点和相对路径使用unittest时的其他所有内容。但是,当直接调用main.py时,main.py有一个相对路径,所有其他文件都有绝对路径。

Interestingly, the rest of the call stack shows an absolute path for main.py, the entry point, and relative paths for everything else when using unittest. When calling main.py directly however, main.py has a relative path and all the other files have absolute paths.

有人知道我们为什么会看到这个吗?区别?当我们在Windows上运行时,不得不跳过大量测试真是令人沮丧。

Does anyone have any idea why we're seeing this difference? It's really frustrating having to skip a load of tests when we're running on windows.

推荐答案

通过在sys中寻找解决方案.path您正在寻找错误的位置。您需要在此处查看os.environ ['PATH']的关键点,因为是Windows Loader选择了错误的 DLL,这是错误的,我的意思是根据有据可查的DLL查找找到的第一个DLL。程序。

By looking for a solution in sys.path you are looking at the wrong place. You need to see what os.environ['PATH'] is at the critical points here since it's the Windows Loader that has picked the 'wrong' DLL, where by wrong I mean the first one it found according to its well documented DLL lookup procedure.

这篇关于运行测试时,Python导入了错误版本的sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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