I am trying to use C-code in Python. I complied the c code in linux and using .so file in windows as I found it on internet. My code of python is given below:
from ctypes import *
add1 = CDLL('./adder.dll')
sum_c = add1.add_int(5,4)
The C-code is a simple adder. This works fine with Python in linux but I get the following error in windows:
test.py line 6, in <module>
add1 = CDLL('./adder.dll')
File "..\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application
Please let me know what is the problem.
Related
import fdb
def main():
con = fdb.connect(dsn='C:\AKINSOFT\Wolvox8\Database_FB\SIRKET.FDB', user='sysdba', password='masterkey')
if __name__ == '__main__':
main()
I wrote a simple code for connecting a Firebird database with Python and fdb. When I run this, I get an winerror 193
Traceback (most recent call last):
File "C:\Users\grand\PycharmProjects\DatabaseTest\main.py", line 9, in <module>
main()
File "C:\Users\grand\PycharmProjects\DatabaseTest\main.py", line 5, in main
con = fdb.connect(dsn='C:\AKINSOFT\Wolvox8\Database_FB\SIRKET.FDB', user='sysdba', password='masterkey')
File "C:\Users\grand\PycharmProjects\DatabaseTest\venv\lib\site-packages\fdb\fbcore.py", line 803, in connect
load_api(fb_library_name)
File "C:\Users\grand\PycharmProjects\DatabaseTest\venv\lib\site-packages\fdb\fbcore.py", line 231, in load_api
setattr(sys.modules[__name__], 'api', ibase.fbclient_API(fb_library_name))
File "C:\Users\grand\PycharmProjects\DatabaseTest\venv\lib\site-packages\fdb\ibase.py", line 1396, in __init__
fb_library = WinDLL(fb_library_name)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 geçerli bir Win32 uygulaması değil
The English error is "[WinError 193] %1 is not a valid Win32 application", and it means you're trying to load a 32-bit DLL in a 64-bit process (or a 64-bit DLL in a 32-bit process). This means that you don't have a valid fbclient.dll on the PATH that matches the bitness of your Python process, but do have one of the wrong bitness.
So, find out if you're using a 32-bit or 64-bit Python (likely 64-bit these days), and use a Firebird installer of the appropriate bitness to install the client library.
If you don't have or don't want fbclient.dll on the PATH, you can also use fdb.load_api(..) or the fb_library_name connection property to specify the path to a fbclient.dll of the appropriate bitness.
As an aside, fdb has been superseded by the firebird-driver library.
it seems subprocess.call function just can be used for the files with '.exe' extension.
This is the code i tried for Firefox.lnk in which this is the same code i tried for a git program that has '.exe' extension and worked without error.
import subprocess
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox')
This is the error I get with Firefox.lnk :
Traceback (most recent call last):
File "C:/Users/m.m/PycharmProjects/untitled5/pros.py", line 2, in <module>
subprocess.call('C:/users/m.m/Desktop/Programs/Firefox.lnk')
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 323, in call
with Popen(*popenargs, **kwargs) as p:
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 775, in __init__
restore_signals, start_new_session)
File
"C:\Users\m.m\AppData\Local\Programs\Python\Python37\lib\subprocess.py",
line 1178, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
Firefox (Without extension) gives me FileNotFoundError: [WinError 2] The system cannot find the file specified.
when i try the code without extension for those programs with '.exe' extension i have no problem but with any program without '.exe' extension i get error... just like firefox that has '.lnk' extension.
To process .lnk files on Windows there is os.startfile() (Windows only) in the standard library.
If you want to add parameters to the command, you can also use the start command. It is a builtin command (no start.exe) therefore a shell is needed to run it.
You can also use shell=True to make this work!
lnk_path = 'C:/users/m.m/Desktop/Programs/Firefox.lnk'
subprocess.call(lnk_path, shell=True)
I am on Windows, and I wish to use Python Bindings for VLC. I've already downloaded the module from https://github.com/geoffsalmon/vlc-python , and did as per the read me. But, still I'm stuck at importing the module. The error looks like this :
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
import vlc
File "c:\python27\python-vlc-1.1.2\vlc.py", line 173, in <module>
dll, plugin_path = find_lib()
File "c:\python27\python-vlc-1.1.2\vlc.py", line 150, in find_lib
dll = ctypes.CDLL('libvlc.dll')
File "C:\Python27\lib\ctypes\__init__.py", line 353, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
Any Solution on where to place the module ? My main aim is to play MP3 audio files through vlc, which would be part of some other activity.
I had same problem.
It turns out for me if you have 64bit python, you need 64bit vlc player.
If you have 32bit python, you need 32bit vlc player.
Hope that works for you too.
I paired with 64bit I solved as follows. Of course
# first set the environment which is required from vlc.py
import os
os.environ['PYTHON_VLC_MODULE_PATH'] = """C:\Program Files\VideoLan"""
os.environ['PYTHON_VLC_LIB_PATH'] = """C:\Program Files\VideoLan\VLC\libvlc.dll"""
# now you can import vlc
import vlc
i'am loading DLLs created by a third party program using ctypes in python 2.7 win8.1 64bit. The code works fine for ~150 loads. This little loop is where i load the DLLS:
for i in os.listdir(self.path):
if '.dll' in i:
self.DLL = cdll.LoadLibrary(self.path+'//'+i)
and fails with the following error msg:
self.DLL = cdll.LoadLibrary(self.path+'//'+i)
File "C:\Users\Johannes\Anaconda\lib\ctypes\__init__.py", line 444, in LoadLibrary
return self._dlltype(name)
File "C:\Users\Johannes\Anaconda\lib\ctypes\__init__.py", line 366, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 1114] A dynamic link library (DLL) initialization routine failed
I changed the order of the DLLs so its definetly not a certain DLL that fails but the ~150th DLL that fails. Is there any limit on loading DLLs?? I cant imagine that's the cause, but also have no idea what i'am doing wrong. Thanks for your help
Have downloaded VLC.py, and placed it in my VLC install directory, where libvlc.dll is also present
On typing import vlc
I get the following error
Traceback (most recent call last):
File "C:\Program Files
(x86)\VideoLAN\VLC\vlc.py", line 88,
in
dll = ctypes.CDLL('libvlc.dll') File
"C:\Python27\lib\ctypes__init__.py",
line 353, in init
self._handle = _dlopen(self._name, mode) WindowsError: [Error 193] %1 is
not a valid Win32 application
Any ideas why?
If needed, my config is:
Win7 pro 64 bit
4GB RAM
Reposting my comment as an answer, since it fixed the problem:
I'm going to guess that the problem is trying to load a 32-bit DLL from a 64-bit process. You may be able to fix it by using a 32-bit Python build.