根据启动器的不同,启动python子进程的行为也有所不同 [英] Launching python subprocess has different behavior depending on launcher

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

问题描述

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

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:

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

当我在DOS命令行上以python.exe main.py(这是Arc的2.6版本的python)运行main.py时,一切正常.但是,如果我使用相同的脚本并将其添加为主应用程序中的工具箱"并从那里启动它,则在import_tests.py文件中的GDAL lib中会出现找不到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 ,我敢打赌,这就是区别是.在这种情况下,您可能需要使用 zc之类的东西.构建/ 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天全站免登陆