Switching from 64bit python.exe to 32bit in mid execution

2.3k views Asked by At

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

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.

1

There are 1 answers

0
Parfait On

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:

  1. Retrieve data with one script (i.e., via ODBC connection) and output into flatfile (txt, csv);
  2. Import flatfile and process data with one script and export into processed flatfile;
  3. Return processed data with another script (i.e., SQL appends into ODBC database).

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'])

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.