问题描述
我有2个单独的python.exe,一个是64位,一个是32位.默认为64位.在中间执行期间,如何使用subprocess
或sys
(或任何相关程序包)将控制从64位切换到32位,仅执行32位代码,然后再将控制切换回64位版本?换句话说,我正在寻找类似...
I have 2 seperate python.exe, one is 64bit one is 32bit. The 64bit is the default one. How can I use subprocess
or sys
(or any relevant package) to switch control during mid-execution from the 64 bit to the 32 bit, execute code only for the 32 bit, then switch control back to the 64bit version? In other words, I'm looking for something like...
if struct.calcsize("P")==8: # check if 64 bit version
# switch to 32 bit version
# ??? new_shell = subprocess.Popen(location of 32 bit python.exe)??? what would go here
# pass a bunch of commands and then switch control back
# to the 64 bit version
另外,我正在使用anaconda,所以我想第一个调用将是切换到32位python.exe环境?在32位过程中,需要导入一些软件包,最后将返回数据.
Also, I'm using anaconda, so I imagine the first call will be for switching to the 32bit python.exe environment? During the 32bit process, some packages need to be imported and data will be returned at the end.
推荐答案
通常不可能在32位和64位模式之间进行中间执行.要解决此问题,请考虑将工作流划分为多个脚本,并在需要时同时具有32位和64位版本的脚本:
Mid-execution between both 32-bit and 64-bit modes mostly likely is not possible. To resolve, consider compartmentalizing your workflow into several scripts and have both 32-bit and 64-bit versions of script when needed:
- 使用一个脚本(即通过ODBC连接)检索数据,然后输出到平面文件(txt,csv);
- 使用一个脚本导入平面文件并处理数据,然后导出到已处理的平面文件中;
- 使用另一个脚本返回处理后的数据(即,SQL附加到ODBC中数据库).
- Retrieve data with one script (i.e., via ODBC connection) andoutput into flatfile (txt, csv);
- Import flatfile and process data with one script and export into processed flatfile;
- Return processed data with another script (i.e., SQL appends into ODBCdatabase).
此外,由于Python的两个位版本都不能彼此交谈",因此您将需要运行两个单独的脚本,这些脚本根据操作系统类型(32位和64位)有条件地被调用.并且参数可以通过子进程传递:
Additionally, because both bit versions of Python cannot "talk" to each other, you will need to run dual, separate scripts that are conditionally called depending on OS type (32-bit vs. 64-bit). And arguments can be passed with subprocess:
#!/usr/bin/python
import struct, subprocess
if struct.calcsize("P") * 8 == 32:
subprocess.call(['C:\pathTo\32bit\python.exe', 'C:\pathTo\32bit\python\32bitscript.py', 'arg1', 'arg2'])
else:
subprocess.call(['C:\pathTo\64bit\python.exe', 'C:\pathTo\64bit\python\64bitscript.py', 'arg1', 'arg2'])
环境路径变量或默认命令行路径将无用,因为它会恢复为一个python版本,而不会显式为另一个版本.因此,需要绝对声明python.exe版本.
Environment path variables or default command line paths will not be useful as it reverts to one python version and not explicitly the other. Hence, the need for absolute declaration of python.exe version.
这篇关于在执行中从64位python.exe切换到32位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!