启动 python 子进程有不同的行为取决于启动器 [英] Launching python subprocess has different behavior depending on launcher

查看:31
本文介绍了启动 python 子进程有不同的行为取决于启动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Python 2.6 启动 Python 2.5.原因是我尝试使用的编译库 (GDAL) 不支持与另一个程序 (ArcGIS) 一起分发的 Python 版本.

I'm attempting to launch Python 2.5 from Python 2.6. The reason for this is a compiled library I'm trying to use (GDAL) isn't supported for the version of Python distributed with another program (ArcGIS).

这就是我正在尝试做的事情.Python 2.6 中的 main.py 文件:

Here's what I'm attempting to do. The main.py file in Python 2.6:

import subprocess
p = subprocess.Popen(['C:\OSGeo4W\gdal_python_exec.bat', 'X:\\local\\import_tests.py'])

gdal_python_exec.bat 是一个 Windows 批处理脚本,它启动我想要的 Python 2.5 版本,同时还设置了一些环境变量:

gdal_python_exec.bat is a windows batch script that fires up the 2.5 version of Python I want while also setting up some environment variables:

@echo off
set OSGEO4W_ROOT=C:\OSGeo4W
PATH=%OSGEO4W_ROOT%\bin;%PATH%
for %%f in (%OSGEO4W_ROOT%\etc\ini\*.bat) do call %%f
@echo on

@C:\OSGeo4W\bin\python.exe %1

import_tests.py 尝试导入 gdal:

import_tests.py tries to import gdal:

try:
    from osgeo import gdal
    raw_input('Imported! (Press enter)')
except Exception, e:
    print(e)
    raw_input('Failed! (Press enter)')

当我在 DOS 命令行中将 main.py 作为 python.exe main.py(这是 Arc 的 2.6 版 python)运行时,一切正常.但是,如果我采用相同的脚本并将其作为工具箱"添加到主应用程序中并从那里启动它,我会在 import_tests.py 中得到 GDAL 库的未找到 DLL"文件!

When I run main.py at a DOS command line as python.exe main.py (that's Arc's 2.6 version of python), things work fine. However, if I take the same script and add it as a 'toolbox' inside the main application and launch it from there, I get a "DLL not found" for the GDAL lib inside the import_tests.py file!

subprocess 是启动不同 Python 解释器的模块时,怎么会发生这种情况?对可能发生的事情有什么想法吗?

How can this happen when subprocess is the module that's launching a different Python interpreter? Any ideas on what could be happening?

编辑:我可以验证 os.environ['PATH'] 变量在两次调用中是否相同.

Edit: I can verify that the os.environ['PATH'] variables are the same in both calls.

Edit2: C:\Program Files\ArcGIS...\Bin 目录包含一个与我的 python 绑定不兼容的 dll.Windows 首先搜索 cwd 并尝试加载该 dll,失败,然后报告找不到 dll"错误.

The C:\Program Files\ArcGIS...\Bin directory contained a dll that wasn't compatible with my python bindings. Windows searched the cwd first and attempted to load that dll, failed, then reported a "dll not found" error.

推荐答案

无论PATH是否正确,一个简单的测试就是切换到不同的任意目录并执行python.exe C:\full\path\to\main.py.如果这重现了问题,那么您就知道这是某种路径问题.

Regardless of whether PATH is correct, a simple test would be to change to a different arbitrary directory and do python.exe C:\full\path\to\main.py. If that reproduces the problem then you know it's some sort of path problem.

检查 sys.path,我敢打赌这就是区别在于.如果是这种情况,您可能需要更改使 Python 代码和库可从 python.exe 访问的方式,或者使用 站点模块 或使用其他类似 zc.buildout/zc.recipe.egg 支持生成带有正确 sys.path 的 console_scripts.

Check sys.path, I'll bet that's where the difference is. If that's the case, you probably need to alter the way you make your Python code and libraries are accessible from python.exe, either using the site module or using something else like zc.buildout/zc.recipe.egg's support for generating console_scripts with the correct sys.path baked in.

这篇关于启动 python 子进程有不同的行为取决于启动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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