Python - unable to import vlc module - python

I just installed python-vlc via pip and when I try
import vlc
The follow error message shows up:
... ...
File "c:\Program Files\Python34\Lib\site-packages\vlc.py", line 173, in <module>
dll, plugin_path = find_lib()
File "c:\Program Files\Python34\Lib\site-packages\vlc.py", line 150, in find_lib
dll = ctypes.CDLL('libvlc.dll')
File "c:\Program Files\Python34\Lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
builtins.OSError: [WinError 126] The specified module could not be found
I am unfamiliar with the ctypes module. What is causing the problem?

The problem has been solved. I was using 64 bit python and 32 bit VLC. Installing a 64 bit VLC program fixed the problem.

python-vlc on Windows needs to load libvlc.dll from VLC. If it's not found in the normal %PATH%, it will try to use pywin32 to look in the registry to find the VLC install path, and fall back to a hard-coded set of
directories after that. The stack trace looks like all of that failed.
Do you have VLC installed?

You are installed VLC 32 bit hence your path goes to program file (x86) and your code search the VLC file in programs file. That's why you are getting this error. To solve this problem we need to install VLC for 64 bits.

I ran into the same problem. To fix it I actually had to install the x86 version NOT the x64bit version... no matter what I did it would not work. I figured this out through looking at the code it was using to find the path. Used a breakpoint to see what it was seeing in the exists flow and what it was searching :
C:<Your Python Path>\Lib\site-packages\vlc.py
if plugin_path is None:
# try some standard locations.
programfiles = os.environ["ProgramFiles"]
homedir = os.environ["HOMEDRIVE"]
for p in ('{programfiles}\\VideoLan{libname}', '{homedir}:\\VideoLan{libname}',
'{programfiles}{libname}', '{homedir}:{libname}'):
p = p.format(homedir = homedir,
programfiles = programfiles,
libname = '\\VLC\\' + libname)
if os.path.exists(p):
plugin_path = os.path.dirname(p)
Hope it helps someone :)

Related

'System.IO.FileNotFoundException: Unable to find assembly' in clr library on Python

I am getting an assembly error in my code using in a .dll file. This is a template code from another thread, and a lot of people claimed that it worked.
import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?',os.path.isfile(file))
clr.AddReference(file)
There is no problem with the file path I guess since it return true on the .isfile function. Here is the output that I'm getting:
Does this filepath exist? True
File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'CPUThermometerLib.dll'.
at Python.Runtime.CLRModule.AddReference(String name)
I have checked multiple threads and none of them is giving a solution. I am using Windows 10, and moreover, my .NET framework version is '4.0.30319.42000'. My laptop processor is an Atom Z3537F.
The clr module is not installed by default on the Windows python installation. You will need to run pip install clr at a prompt to have it added and then your code will be able to import it properly.
You need to provide the full path to the dll file, not just the file name.
If the dll file is in the same directory as your Python script, you can get the full path by using os.path.abspath('CPUThermometerLib.dll').
Full code:
import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?', os.path.isfile(file))
clr.AddReference(os.path.abspath(file))

OpenALPR Python binding failing when trying to load dll

After downloading the precompiled binaries for Windows of the openALPR library, running the setup.py included in the source code to install it, and running the python_test.bat included in the precompiled binaries directory I get the following error:
File "C:\Users\rhenriquez\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openalpr\openalpr.py", line 51, in __init__
self._openalprpy_lib = ctypes.cdll.LoadLibrary("libopenalprpy.dll")
File "C:\Users\rhenriquez\AppData\Local\Programs\Python\Python37-32\lib\ctypes\__init__.py", line 434, in LoadLibrary
return self._dlltype(name)
File "C:\Users\rhenriquez\AppData\Local\Programs\Python\Python37-32\lib\ctypes\__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: OSError: [WinError 193] %1 is not a valid Win32 application
When I started to run the script at first the error was WinError 126 and then I noticed that the dll that the script was trying to call (openalprpy.dll) didn't had "lib" at the beginning of its name, so I added it to the dll's name. Then it started to send me WinError 193, and I've been clueless on what else to do or what am I doing wrong from then on.
Any help would be appreciated.
EDIT: So I tried this with the 32-bit version and it gives the same error you are encountering. This appears to be an issue with trying to import a 32-bit .dll while using 64-bit python, or vice-versa, as seen in Python Ctypes Load Library
/EDIT
I did get it to work on my system... with a few modifications, this package is not as "plug and play" as it should be.
I don't know where I went right, so I'll just list what I did:
Download the pre-compiled biniaries from releases (I used openalpr-2.3.0-win-64bit.zip) https://github.com/openalpr/openalpr/releases
Download the project itself, https://github.com/openalpr/openalpr
Unzip both.
Goto the bindings folder in openalpr-master cd C:\openalpr-master\openalpr-master\src\bindings\python and run python setup.py install to make the bindings.
Then navigate to the project folder in site-packages, most likely C:\Users\rhenriquez\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openalpr\ and open openalpr.py in IDLE,
here you can change line 51 from self._openalprpy_lib = ctypes.cdll.LoadLibrary("libopenalprpy.dll") to self._openalprpy_lib = ctypes.cdll.LoadLibrary("openalprpy.dll") since it appears other links are broken if you change the file name.
I also found it beneficial to change line 90 to except Exception: since it did not want to import correctly and was not raising an ImportError.
After that the python_test.bat worked correctly.
Namespace(config='openalpr.conf', country='us', plate_image='samples/us-1.jpg', runtime_data='runtime_data')
Using OpenALPR 2.3.0
Image size: 497x372
Processing Time: 561.825989
Plate #1
Plate Confidence
- THECAR 92.207481
- THEGAR 81.348961
- HECAR 80.229317
- TMECAR 78.159492
- THE0AR 77.702461
- THECAB 77.389000
- THEAR 76.510017
Press any key to continue . . .

ImportError: No module named _analog_swig

I am having issues getting python to import the _analog_swig gnuradio module in order to run gnuradio code on a Windows 8.1 64bit machine.
Some background: I am running Python 2.7.10 (installed in C:\Python27) and have installed the latest gnuradio binary (v3.7.11.1/v1.3 64-Bit Any CPU) from here: http://www.gcndevelopment.com/gnuradio/downloads.htm. I have installed gnuradio to C:\Program Files\GNURadio-3.7 .
I can run gnuradio companion and run flowgraphs from GRC successfully (which calls "C:\Program Files\GNURadio-3.7\bin\run_gr.bat" gnuradio-companion.py).
I have added & verified the following system variables are set:
Path: C:\Program Files\GNURadio-3.7\bin
PYTHONPATH: C:\Program Files\GNURadio-3.7\lib\site-packages
GRC_BLOCKS_PATH: C:\Program Files\GNURadio-3.7\share\gnuradio\grc\blocks
Now to the problem: If I run e.g. CMD and type:
python C:\test\top_block.py
I am returned the following ImportError:
File "C:\test\top_block.py", line 22, in <module>
from gnuradio import analog
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\__init__.py", line 33, in <module>
from analog_swig import *
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\analog_swig.py", line 17, in <module>
_analog_swig = swig_import_helper()
File "C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog\analog_swig.py", line 16, in swig_import_helper
return importlib.import_module('_analog_swig')
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named _analog_swig
The folder content of C:\Program Files\GNURadio-3.7\lib\site-packages\gnuradio\analog is as follows:
Comparing this to the folder content on a linux machine, which has a working install of gnuradio that works with python as I want it:
The difference seems to be that the folder in windows contains only a _analog_swig.pyc file, whereas the folder in linux contains a _analog_swig_.so file.
Any idea why the _analog_swig module can apparently not be imported in windows?
My plan is to be able to run gnuradio code directly from my python interpreter and being able to create compiled gnuradio executables so any help on how this could be fixed is much appreciated.
I've been struggling with this for the past few days, but I finally figured it out. I was trying to run GnuRadio Companion generated code in IDLE and also in PyCharm. I kept failing miserably with this same error. I finally figured it out:
-As Flexo says, the PYD file (_analog_swig.pyd) is actually a Windows DLL. The error makes it sound like Python is not finding that file, but that is not at all what was happening. The PYD file, being a DLL, has dependencies itself. Python is able to find _analog_swig.pyd just fine, but it could not find the DEPENDENCIES of that library.
-To verify if that's what wrong in your installation, download and use DependencyWalker (Google it) to check if your system can find the dependencies to _analog_swig.pyd.
-The fix for me was to add the GnuRadio-3.7/bin folder to my PATH environment variable. Inside that folder are a number of DLLs that the _analog_swig.pyd library needs to load. If you don't have the folder in your PATH, the module will fail to load in Python and throw the error you see above.
-I see that you verified that this folder is in your PATH, so this is apparently not the same problem, although your symptoms are exactly the same as mine. i.e. the GRC code would run just fine when you start with "run_gr.bat", but not when you run from a normal CMD window.
Hopefully that helps someone else that wants to use GNURadio Python code on Windows.
Friend,
As you mentioned, the GNU Companion calls \bin\run_gr.bat gnuradio-companion.py. That batch script does quite a bit of work on windows environment variables (try opening it in a text editor if you're curious).
In a sense, the run_gr.bat script puts together a temporary, custom python workspace for gnuradio so it can import anything it needs. It receives python scripts to run in this environment as command line arguments; hence, you can use it to run any GNU radio python code you want in your windows command prompt. Generally, you would call
<gnuradio_install_path>\bin\run_gr.bat <gnu_radio_code>.py
To test your import, you can try
# test.py
from gnuradio import analog
try calling the following from the command prompt, in the test.py directory:
<gnuradio_install_path>\bin\run_gr.bat test.py

Missing files for `magic` library on Windows

I need to get mime type for some files on windows, so i've installed python-magic (on 32-bit python 2.7.3).
It depends on unix magic library.
Author instructs to get regex2.dll, zlib1.dll and magic1.dll from gnuwin32 project.
So i saved the files to a folder and added the folder to my system PATH.
Now when i execute magic methods, i get missing file exception:
import magic
print(magic.Magic())
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 367, in <module>
test_magic()
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 364, in test_magic
print(magic.Magic())
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 52, in __init__
magic_load(self.cookie, magic_file)
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 188, in magic_load
return _magic_load(cookie, coerce_filename(filename))
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 139, in errorcheck
raise MagicException(err)
magic.MagicException: could not find any magic files!
DLLs are in the PATH, i tried debugging and magic1.dll is located correctly, but somewhere inside library throws an exception.
Inside the gnuwin32 package i've found magic and magic.mgc. I placed them to the same folder, and got WindowsError: [Error 126] on
libmagic = None
# Let's try to find magic or magic1
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
# This is necessary because find_library returns None if it doesn't find the library
if dll:
libmagic = ctypes.CDLL(dll)
This obviously happens because python tries to open magic file as dll, which is plain text. After adding .dll to filenames in the code i get the same magic.MagicException: could not find any magic files!.
What files am i missing?
UPDATE:
C:\Users\Admin>file C:\123.zip -m magic
file: could not find any magic files!
C:\Users\Admin>file C:\123.zip -m "C:\#DEV\#LIB\#Magic\GetGnuWin32\bin\magic"
C:\123.zip; ASCII text, with no line terminators
C:\Users\Admin>cd C:\#DEV\#LIB\#Magic\GetGnuWin32\bin
C:\#DEV\#LIB\#Magic\GetGnuWin32\bin>file C:\123.zip -m magic
C:\123.zip; ASCII text, with no line terminators
UPDATE 2 (SOLVED):
print(magic.Magic())
magic.MagicException: could not find any magic files!
print(magic.Magic(magic_file = 'magic'))
<magic.Magic instance at 0x02A5E198>
just had to specify file explicitly
For future google visitors: Another solution is setting the %MAGIC% enviroment variable in the systems setting to point to the magic file, for me it was:
"c:\Program Files (x86)\GnuWin32\share\misc\magic"
No need to hardcode the path in your program!
Path to magic file has to be explicitly passed to the constructor.
magic_object = magic.Magic(magic_file = 'path_to_magic_files/magic'))
As the python-magic problems seems to be quite common, here a working solution fo future googlers:
After testing most solutions without altering the source-code, I found the following to get python-magic working out of the box:
Install GnuWin32 file first
Set the environment variable MAGIC=path\to\gnuwin32\share\misc\magic
Assure all installed executables/libraries to be accessible via the
PATH
Install python-magic via pip
Please try install this package:
pip install python-magic-bin
try this:
pip uninstall python-magic
pip uninstall python-magic-bin
pip install python-magic
pip install python-magic-bin
which means install python-magic-bin after python-magic
reference:https://github.com/ahupp/python-magic/issues/248

MemoryLoadError when trying to run py2exe application

I am attempting to bundle up a Python application using py2exe 0.6.9 and Python 2.6.4 on Windows. While the executable runs just fine on the system I used to build it, it fails when I attempt to run it on another system:
C:\Documents and Settings\Administrator\Desktop\dist>.\backend.exe install
Traceback (most recent call last):
File "boot_service.py", line 6, in <module>
File "zipextimporter.pyo", line 82, in load_module
File "win32serviceutil.pyo", line 9, in <module>
File "zipextimporter.pyo", line 98, in load_module
ImportError: MemoryLoadLibrary failed loading win32api.pyd
I have a strong hunch that I'm missing a library, but I'm unsure which—especially since the dependency checker isn't flagging anything as missing on the target system. How should I proceed?
same question as https://stackoverflow.com/questions/1979486/py2exe-win32api-pyc-importerror-dll-load-failed.
look at setup.py, just excludes these dlls which are included in the system.
'dll_excludes': [ "mswsock.dll", "powrprof.dll" ]
it will help you!
You can do the dll and dependency check manually. When you generate the exe i am sure you have noticed that it prints out the dll's that are required/used. Check on the system, where the exe is not working, if the dll's exist and if they are the same version.
One other thing. Are you copying the whole dist folder to the system or just the exe, because you need the whole dist folder and not just the exe.
Are you doing the py2exe conversion on a 64bit/vista?
I've been writing my code on a vista64bit, and the .exe files I create usually do not work on 32bit XP (those are the two machines i have on hand).
The .exe helpfully throws out a text file with the traceback, and it appears that the 64bit windows uses the win32api.dll. I assume this is a .dll used by 64bit windows to replicate 32bit OS behaviour, so I simply take the same script and do the conversion on the 32bit XP. Hope that helps.
Just for anyone who will come to here in the future. If you are using any kind of win32 library and u stuck with this type of errors you can do the following steps:
The problem issue is that there is a conflict between win32 functions dll files and the py2exe automatically dll files. So to solve this conflict you have to know your functions required dll files, then exclude these files from the setup options
Example:
According to the following code:
import win32crypt
win32crypt.CryptUnprotectData(...)
I used the CryptUnprotectData function so I searched for the CryptUnprotectData required dll and I found the following info enter link description here , As u can see,
"Crypt32.dll" is required.
so I edit my setup.py to be look like that
includes = ["win32crypt"]
dll_excludes=["Crypt32.dll"]
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True,'dll_excludes': dll_excludes,'includes': includes}})
and it worked perfectly.

Categories