How to use a C dll in python - c++

I am trying to use a C++ dll in python. I am running python 2.7 on Windows server 2012; both are 64bit.
To create a dll, I followed the directions on "Walkthrough: Creating and Using a Dynamic Link Library (C++)" on http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx. I used their example code. I am new to dlls and python so I thought I would start with examples.
My python code:
from ctypes import *
hw = CDLL("Y:\dll_check\MathFuncsDll.dll")
print "HelloWorld"
I get the following error:
Y:\dll_check> python .\MathFuncsMain/py
Traceback (most recent call last):
File ".\MathFunsMain.py"
libimgr = CDLL("Y:\dll_check\MathFuncsDll.dll")
File "C:Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application
What is causing my error?

use 32 bit python version and compile to resolve the issue.
Reason(probably) : even though your machine is 64-bit; the dll you have created is a 32 bit one. bcz through visual studio you can select and create only "Win32 application".

Related

import pjsua: DLL load failed: %1 is not a valid Win32 application

When i try to import pjsua it's showing below error in windows python x64 bit
Traceback (most recent call last):
File stdin, line 1, in
ImportError: DLL load failed: %1 is not a valid Win32 application.
I'm using Python 2.7 and these are for pjsua build generation.When i search for this issue some one asked to install opencv and set the env path.I tried it but no luck.
Please help us to solve this.
I'm referring https://trac.pjsip.org/repos/wiki/Python_SIP/Build_Install.

Executing Python file in Visual Studio 2017 works, but not when I execute it via cmd

I have a Visual Studio project. It contains several Python files. One of them is the main.py. It is working great when I right-click the main.py file in Visual Studio, and start it "with/without Debugging".
But if try to run it via cmd like this: "python main.py"
it gives me this error:
Traceback (most recent call last):
File "main.py", line 6, in <module>
from units.predefined import define_units
ImportError: No module named units.predefined
Somehow it does not find the modules when I try to execute the Python file via cmd. Does someone know why it is not finding the modules? It would be great if someone could help me :)

Pyinstaller-generated exe cannot run on another computer

My computer's OS is 64-bit win 10. Python 2.7, 32-bit.
My code is plot.py, simple as below:
import matplotlib.pyplot as plt
import FileDialog
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
When I execute pyinstaller -F plot.py, the generated
plot.exe works as expected on my current computer. However, I get an error if I try to run it on a different 32-bit Windows 7 computer:
Traceback (most recent call last):
File "site-packages\GUI_tempCtrl\plot.py", line 3, in <module>
File "lib\site-packages\matplotlib\pyplot.py", line 3147, in plot
File "lib\site-packages\matplotlib\pyplot.py", line 928, in gca
File "lib\site-packages\matplotlib\pyplot.py", line 578, in gcf
File "lib\site-packages\matplotlib\pyplot.py", line 527, in figure
File "lib\site-packages\matplotlib\backends\backend_tkagg.py", line 84, in new
_figure_manager
File "lib\site-packages\matplotlib\backends\backend_tkagg.py", line 92, in new
_figure_manager_given_figure
File "lib\lib-tk\Tkinter.py", line 1814, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Users/ADMINI~1/AppData/Local/Temp/lib/tcl8.5 C:/Users/Administrator/lib/t
cl8.5 C:/Users/lib/tcl8.5 C:/Users/Administrator/library C:/Users/library C:/Use
rs/tcl8.5.15/library C:/tcl8.5.15/library
This probably means that Tcl wasn't installed properly.
Failed to execute script plot
You can try downgrading pyinstaller to v3.1 and rebuilding your exe file on your first computer to solve this issue.
Alternatively, you can fix it on your second computer by setting the TCL_LIBRARY environment variable to C:\Python27\tcl\tcl8.5\ (or wherever tcl8.5 is located)
PyInstaller has a known issue with Tkinter applications on Windows 7 and Windows XP.
Since this issue has gone unfixed for two years, I've gone ahead and started a bounty. Until the issue is fixed, there are a few workarounds you can try:
Workaround 1 - Manually copy missing files
As mentioned in a related issue, you can manually copy the missing files from your local Python installation.
Find your local Python installation. (%LocalAppData%\Programs\Python)
Make a copy of the missing folder (...\Python36-32\tcl\<missing_folder>)
Move the copy to your application's tcl folder (...\dist\<app_name>\tcl\<missing_folder>)
Workaround 2 - Run with --onefile
Running PyInstaller in --onefile mode seems to avoid this issue.
However, note that running in single file mode will increase startup time.
Workaround 3 - Downgrade to PyInstaller 3.1.0
pip install pyinstaller==3.1.0
According to ugoertz, downgrading to PyInstaller 3.1.0 resolved the issue.
Downgrading to 3.1.0 (and also downgrading setuptools to 19.2 because of the problem described in #1941) fixed the issue for me.

Building PyV8 (a C++/Python project) from source in Windows

How do you get started running (and debugging) a C++/Python combined project?
So far I've started with the basics:
Checking out the project from source (http://code.google.com/p/pyv8/source/checkout)
Copying a helloworld.py example into the directory with PyV8, so it can import PyV8.
This doesn't work, of course:
Traceback (most recent call last):
File "helloworld.py", line 4, in <module>
import PyV8
File "C:\Users\Default User\Documents\pyv8-read-only\PyV8.py", line 32, in
<module>
import _PyV8
ImportError: No module named _PyV8
The next step is probably to compile the cpp files and somehow make them available to Python as a library, but I'm not sure how to do that from the command line. There appears to be a VS project but I don't have Visual Studio.
(It would be way more awesome if anyone could also figure out how to debug both the C++ and Python parts in Eclipse)
Help! Thanks.

libsvm in Python : getting error

I am new to Python. I wanted to use svm for my project. I was using libsvm 3.12 but when i do ..
from svm import *
then i get the following error :
Traceback (most recent call last):
File "C:/Python27/main.py", line 31, in
from svm import *
File "C:/Python27\svm.py", line 12, in
libsvm = CDLL(find_library('libsvm'))
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
What should I do?
The libsvm README file suggested that I copy libsvm.dll into the system32 folder, which I did .. but still I am getting the error. I am using 64 bit Python. Maybe that is causing the problem?
I just had the same issue.
As Velociraptors posted in VLC Python Bindings -- Error 193:
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.
We solved the issue by downloading libSVM for python from http://www.lfd.uci.edu/~gohlke/pythonlibs/#libsvm