I want to control from python 64bit to 32bit (dll refer to 64bit version)

Asked 2 years ago, Updated 2 years ago, 29 views

I would like to control the 64-bit Python to 32-bit Python.
I wish I could simply control it with 64-bit Python, but the equipment I want to control only supports 32-bit and the controller only has 64-bit versions.

image:
Automation software--64bit Python--32bit Python-->Equipment you want to control

The equipment you want to automate can be controlled by win32com, but only 32bit is supported.You can control what equipment you want to control from 32-bit Python.

The problem is that I am running (subprocess) from 64-bit Python to 32-bit Python, but when 32-bit Python imports win32com, I refer to the 64-bit Python dll and get an error.

Run from 64bit

import subprocess

py_path=r "C:\path\to\32bit\Python\python.exe"
cmd=r "C:\users\py\command.py"
proc=subprocess.Popen(
            [py_path, cmd],
            shell=True,
            stdin=subprocess.PIPE,
            stdout = open('out.txt', 'w',
            stderr = open('err.txt', 'w', 
            encoding = 'utf-8'
)

command.py

import win32 com.client
...

If you do the above, the following error will not work as of import on command.py:

Traceback (most recent call last):
  File "C:\\users\\py\\command.py", line 1, in <module>
    import win32 com.client
  File "C:\Path\to\64bit\Python\lib\site-packages\win32com\_init__.py", line 5, in<module>
    import win32 api,sys,os
ImportError: DLL load failed while importing win32 api: %1 is not a valid Win32 application.

In fact, when I checked sys.path on command.py, I went to see the 64-bit version as follows:

['C:\\users\\py',
 US>'C:\\Path\\to\\32bit\\Python\\python38.zip',
 US>'C:\\Path\\to\\64bit\\Python\\DLLs',
 US>'C:\\Path\\to\\64bit\\Python\\lib',
 US>'C:\\Users\\user\\AppData\\Local\\Programs\Python',
 US>'C:\\Path\\to\\64bit\\Python',
 'C:\\Path\\to\\64bit\\Python\\lib\\site-packages',
 US>'C:\\Path\\to\\64bit\\Python\\lib\\site-packages\\win32',
 US>'C:\\Path\\to\\64bit\\Python\\lib\\site-packages\\win32\\lib',
 US>'C:\\Path\\to\\64bit\\Python\\lib\site-packages\\Pythonwin' ]

Therefore, I tried to prioritize the 32-bit version using sys.path.append() on command.py, but there was no change in the error.
Also, I tried to empty sys.path ([]) on command.py and put in only the 32-bit version of the pass, but now I can't find win32com.

What should I do?

python windows

2022-09-30 18:12

1 Answers

Now that I've solved it myself, I'll write down my own solution.

Applications running 64-bit Python seemed to have some PATHs added automatically within the application, and after erasing those PATHs, I was able to create and pass environment variables for 32-bit to env arguments in subprocess.

importos
new_env=os.environ.copy()

delete_name = ['xxx', 'yyyy'] #xxx,yyy is the name of the application
add_path_list = [r'C:\path\to\32bit\python']
new_env_dict = {
    'PYTHONHOME':r'C'\path\to\32bit\python']
}

path_list = [ ]
for newpath in new_env ['PATH'].split(';'):
    flag = 0
    for target in delete_name:
        if target in newpath:
            flag = 1

        if flag == 0:
            path_list.append(mypath)

for newpath in add_path_list:
    path_list.append(newpath)

path_list=filter(lambdaa:a!=', path_list)
path_str=";".join(path_list)
new_env['PATH'] = path_str

for key, value in new_env_dict.items():
     new_env[key] = value

Pass the new_env created as the with the argument env in subprocess.

import subprocess

py_path=r "C:\path\to\32bit\Python\python.exe"
cmd=r "C:\users\py\command.py"
proc=subprocess.Popen(
            [py_path, cmd],
            shell=True,
            stdin=subprocess.PIPE,
            stdout = open('out.txt', 'w',
            stderr = open('err.txt', 'w', 
            encoding = 'utf-8',
            env = new_env,
)

Now I can run 32-bit Python from 64-bit Python and use 32-bit dll.

In addition, in the above environment variable settings, only Python paths used by applications are
The operation itself was possible by deleting it.

Thank you.


2022-09-30 18:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.