Loading a .dll file in Windows Python 3.4 - python

Currently, I am using Python 3.4 out of the Anaconda Suite. I have a file called "libPiCam.dll" within my working directory and I am trying to load it in this fashion:
from ctypes import cdll
def load(x):
"""Loads DLL library where argument is location of library"""
x = cdll.LoadLibrary(x)
return x
lib = load("PiCamLib")
But when doing that, I get this tracer back
Traceback (most recent call last):
File "<ipython-input-27-8d86fac88101>", line 1, in <module>
runfile('C:/Users/User/Documents/Python Scripts/PicamCode/PixisTest.py', wdir='C:/Users/User/Documents/Python Scripts/PicamCode')
File "C:\Users\User\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Users\User\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/User/Documents/Python Scripts/PicamCode/PixisTest.py", line 49, in <module>
piFunction = load(piFunctionLib)
File "C:/Users/User/Documents/Python Scripts/PicamCode/PixisTest.py", line 32, in load
x = cdll.LoadLibrary(x)
File "C:\Users\User\Anaconda3\lib\ctypes\__init__.py", line 429, in LoadLibrary
return self._dlltype(name)
File "C:\Users\User\Anaconda3\lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
What confuses me the most is that I have a file called "PiCam.dll" and load it in this fashion:
picamLibrary = 'picam'
picam = cdll.LoadLibrary(picamLibrary)
And loads using th eLoad method above. This works without error. I tried shaping my call to load libPiCam to be the same way, but I get the same error. As background, I created and compiled the "libPiCam.dll" file from a C++ file in order to wrap it, allowing me to call C++ functions from Python (I followed Calling C/C++ from python?).
So, what do I do?

I notice the naming isn't quite consistent: issue with libPiCam.dll vs "PiCamLib" doesn't work, while PiCam.dll vs "picam" does. Is it a an issue with case? Have you tried "libpicam"?
Otherwise, I wouldn't be surprised with it being an issue with the Anaconda distribution itself. I've had several issues that turned out to be bugs that occur when Anaconda interacts with windows. Ie. the c++ compiler not working properly, not being able to import pywin32 unless I run the .py script with IPython for some reason, etc.

Related

midi2audio/FluidSynth: [WinError 2] The system cannot find the file

I get a FileNotFoundError error while trying to call midi2audio.
I am working with Python 3.6 - Anaconda - Windows 10.
My .py file is in the same folder, together with the .sf2 and the .mid files.
My code is:
from midiutil.MidiFile import MIDIFile
from midi2audio import FluidSynth
soundfont = 'C:\\Users\\matteo.stante\\Documents\\4_Python_Scripts\\experiments\\JR_sax.sf2'
filepath = 'C:\\Users\\matteo.stante\\Documents\\4_Python_Scripts\\experiments\\input.mid'
fs = FluidSynth(sound_font=soundfont, sample_rate=22050)
fs.midi_to_audio(filepath, 'output.wav')
I would expect a .wav file in my folder being created.
I get instead:
runfile('C:/Users/matteo.stante/Documents/4_Python_Scripts/experiments/AudioBello.py', wdir='C:/Users/matteo.stante/Documents/4_Python_Scripts/experiments')
Traceback (most recent call last):
File "<ipython-input-43-cdda6b922219>", line 1, in <module>
runfile('C:/Users/matteo.stante/Documents/4_Python_Scripts/experiments/AudioBello.py', wdir='C:/Users/matteo.stante/Documents/4_Python_Scripts/experiments')
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/matteo.stante/Documents/4_Python_Scripts/experiments/AudioBello.py", line 26, in <module>
fs.midi_to_audio(filepath, 'output.wav')
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\site-packages\midi2audio.py", line 46, in midi_to_audio
subprocess.call(['fluidsynth', '-ni', self.sound_font, midi_file, '-F', audio_file, '-r', str(self.sample_rate)])
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 210, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\matteo.stante\AppData\Local\Continuum\anaconda3\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Impossibile trovare il file specificato
soundfont = 'C:\\Users\\matteo.stante\\Documents\\4_Python_Scripts\\experiments\\JR_sax.sf2'
filepath = 'C:\\Users\\matteo.stante\\Documents\\4_Python_Scripts\\experiments\\input.mid'
Windows: \\ (why 2? Because \ is used by formatting like \n \r ... so you need 2)
Linux: /
If the file is in the same folder, then
soundfont = 'R_sax.sf2'
filepath = 'input.mid'
EDIT
I tried it on my PC (windows 10) and I get same error. I documentation I didnt find any mention about OS, but there is obviously some linux commands.
Try this on Linux OS
EDIT2
I put little effort and tried it on Linux too.
1) Error the same - File not found
2) I found out you have to put folder somewhere on the path -command: $PATH
I put it to /usr/local/bin/fluidsynth
3) New error: Permission denied (couldnt pass it even with root)
4) I found that I dont have fluidsynth installed
command: apt install fluidsynth
5) your script worked (kind of, output.wav generated)
Possible solution
I believe that the problem - after installing fluidsynth on the linux - that your solution for Windows will be solved after installing fluidsynth on windows.
http://www.fluidsynth.org/

"ImportError: No module named PyQt4.QtCore" Despite PyQt5 being imported

I've been going through this package of python scripts:
https://github.com/werdeil/clementine-last-export
To convert from pyqt4 to pyqt5 as pyqt4 is no longer supported on my OS.
I did both a manual conversion and a conversion using the following script:
https://github.com/rferrazz/pyqt4topyqt5
Both the script and the conversion from pyqt4->pyqt5 seem straight-forward but once I am done converting the scripts I still get:
Traceback (most recent call last):
File "gui.py", line 374, in <module>
main()
File "gui.py", line 355, in main
cleg = ClemLastExportGui()
File "gui.py", line 45, in __init__
self.load_config()
File "gui.py", line 350, in load_config
self.config = pickle.load(open(self.configfile))
File "/usr/lib64/python2.7/pickle.py", line 1384, in load
return Unpickler(file).load()
File "/usr/lib64/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/usr/lib64/python2.7/pickle.py", line 1139, in load_reduce
value = func(*args)
ImportError: No module named PyQt4.QtCore
despite the fact that no refrences to pyqt4 are even active in the source:
~/projects/clementine-last-export $ grep -i pyqt4 -r *
README.txt:pyqt4 stopped being supported by most Linux Operating Systems
I looked through the libraries on my system and pyqt4 is entirely gone, so that doesn't seem likely to be the cause.
Could someone take a look at my code here:
https://github.com/davidsmind/clementine-last-export
Or point me in the right direction if it is a system issue?

Python bindings for VLC

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

Python pyinstaller and translit module

I'm using module transliterate.py from standard translit python libraries.
When I compile my GUI application, it raises the next error.
Traceback (most recent call last):
File "C:\Users\...\...\build\Rman\out00-PYZ.pyz\addobjectdialog", line 265, in OnTextName
File "C:\Users\...\...\build\Rman\out00-PYZ.pyz\core.utils", line 536, in translit
File "C:\Users\...\...\build\Rman\out00-PYZ.pyz\transliterate.utils", line 41, in translit
File "C:\Users\...\...\build\Rman\out00-PYZ.pyz\transliterate.utils", line 29, in ensure_autodiscover
File "C:\Users\...\...\build\Rman\out00-PYZ.pyz\transliterate.discover", line 27, in autodiscover
WindowsError: [Error 3] : 'C:\\...\\...\\AppData\\Local\\Temp\\_MEI11122\\transliterate\\contrib\\languages/*.*'
How I understand, I have to place
that lib somewhere within my application. But I just compile one .exe file, so wouldn't like to keep yet one file with my exe.
BTW, app is ran under Win 7 / 32bit.
Thanks.
That may be one of those libraries that you have to include explicitly. I know I had to do that from time to time with py2exe. See the following docs for more information:
http://pythonhosted.org/PyInstaller/#options-for-finding-imported-modules-and-libraries

Python, cudamat, and Spyder

I'm trying to use the cudamat library and I can't figure out how to use it from within Spyder. I have included the cudamat folder contents in my working directory, but when I try:
import cudamat as cm
I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cudamat/__init__.py", line 1, in <module>
from cudamat import *
File "cudamat/cudamat.py", line 7, in <module>
_cudamat = ct.cdll.LoadLibrary('libcudamat.so')
File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libcudamat.so: cannot open shared object file: No such file or directory
When I run the my script using python from a console window, it works fine. To the best of my knowledge, I have set the working directory of Spyder to the correct location, which contains libcudamat.so.
Any ideas?
(Spyder dev here): You need to start Spyder from a console, so that it can grab its environment variables. I guess cudamat is not installed in a regular location and that's why Spyder can't find it by default.
Note: This is an Spyder limitation (i.e. not being able to read environment variables) that we plan to fix in the future.

Categories