Adding to Python Path DLLs - python

I am currently trying to use the camera module from Pygame, a set of Python modules designed for writing video games. Trying to initialize the camera gives the error
from VideoCapture import vidcap as vc
ModuleNotFoundError: No module named 'VideoCapture'
I am trying to resolve this error using the instructions from python pygame.camera.init() NO vidcapture, but I am not sure how to execute step 2 in the recommended solution, which says to add the corresponding version of dll from a downloaded program to "your python path"\DLLs. I understand this is a reference to the python system path, but am not sure what it means to add the dll.

"your python path" is the path of your python installation. DLLs is the directory DLLs under that very path.
So if your python installation is at (e.g.)
C:\Users\max.power\AppData\Local\Programs\Python\Python37-32
then the "your python path"\DLLs directory refers to
C:\Users\max.power\AppData\Local\Programs\Python\Python37-32\DLLs
Also note that the camera module is marked as experimental, only works with v4l2 cameras and only supports linux out of the box, so I don't know if the answer you linked works at all.

Related

Package python file with imgs and modules to an executable file? [duplicate]

I created a game using pygame (python2.7) and tried to convert it using py2exe.
These are the modules I used:
pygame,Tkinter,random
here's my "setup.py":
from distutils.core import setup
import py2exe
setup(options={
"py2exe":{
"includes": ["Tkinter","pygame","random"]
}
}
)
when I try to run the .exe file I get this Error:
NotImplementedError: font module not avaible
(ImportError: DLL load failed: module couldn't be found
What do I have to change?
There's two things to check here. First, ensure that you are using 32-bit python and 32-bit pygame. Pygame only plays nice with 32-bit python, and you're opening a can of worms if you ignore that. The other thing to check is to make sure that all the modules are spelt the way that they are spelt on your system when you load in the dlls. (A common suspect is that Tkinter has an upper case module name and this might throw something off)

Using embedded python, loading a *.pyd outside of DLLs directory fails

I have a C++ application (X-Plane) for which there is a plugin which permits the use of python scripts (XPPython3 plugin). The plugin is written in C, using python CAPI, and works great, allowing me to write python scripts which get executed within the C++ application.
On Windows 10, I want to extend my python features by importing imgui. I have a python cython-built pyd file (_imgui.cp39-win_amd64.pyd).
If I place the pyd file in C\Program Files\Python39\DLLs, it works as expected: C++ application calls CAPI to python, which loads script which imports and executes imgui code.
If I place the pyd file anywhere else, embedded python either reports "module not found" -- if the pyd isn't on sys.path(), or if it is on sys.path():
ImportError: DLL load failed while importing _imgui: The parameter is incorrect.'
Changes using: os.add_dll_directory(r'D:\somewhere\else')
Does not effect whether the module is found or not, nor does it change the 'parameter incorrect' error. (see https://bugs.python.org/issue36085 for details on this change. -- my guess is add_dll_directory changes lookup for DLLs, but not for pyd?) sys.path appears to be used for locating pyd.
Yes, the pyd is compiled with python3.9: I've compiled it both with mingw and with visual studio toolchains, in case that might be a difference.
For fun, I moved python-standard _zoneinfo.pyd from Python39\DLLs and it fails in the same way in embedded python: "The parameter is incorrect". So, that would appear to rule out my specific pyd file.
The key question is/are:
Other than placing a pyd file under PythonXX\DLLs, is there a way to load a PYD in an embedded python implementation? (I want to avoid having to tell users to move my pyd file into the Python39\DLLs directory... because they'll forget.)
Note that using IDLE or python.exe, I can load pyds without error -- anywhere on sys.path -- so they don't have to be under Python39\DLLs. It's only when trying to load from embedded python that the "Parameter is incorrect" appears. And of course, this works flawlessly on Mac.
(Bonus question: what parameter? It appears to be python passing through a windows error.)
There seems to be a simple answer, though I suspect it's better characterized as a python bug.
There is nothing magical about Python39\DLLs directory.
The problem is using absolute vs relative paths in sys.path.
Python can find modules using absolute or relative paths. So if zippy.py is in folder foobar,
sys.path.append('foobar')
import zippy
# Success
Python and find, BUT NOT LOAD pyd files using relative paths. For example, move _zoneinfo.pyd from PythonXX\LDDs to foobar
sys.path.append('foobar')
import _zoneinfo
# ImportError: DLL load failed while importing _zoneinfo: The parameter is incorrect.'
Instead, use absolute path, and it will find and load PYD:
sys.path.append(r'c:\MyTest\foobar')
import _zoneinfo
# Success
So, there is actually a way to do this—that is, ship your application with the desired libraries. The solution is to use an embedded distribution and ship this with your application. You can find the correct distribution on the official Python download page corresponding to your desired version (here's the link to the lastest 3.9 release which seems to be what you're using: https://www.python.org/downloads/release/python-392/). Look for the Windows Embeddable Package.
You can then simply drop in your .pyd file alongside the standard library files (note that if your third-party library is dependent on any other libraries, you will have to include them, as well). Shipping your application with an embeddable distribution should not only solve your current issue, but will also mean that your application will work regardless of which version of Python a user has installed (or without having Python installed at all).

Python gives error when importing simple C extension module

On windows I have built a very simple "hello world" C extension (the file hello.c from this site https://gist.github.com/physacco/2e1b52415f3a964ad2a542a99bebed8f). Using VS2015 I successfully obtain hello.dll. The problem is that I can't figure out how to import this file/module.
In the python shell (python 3.7) I have made sure that I'm in the same folder as the hello.dll. I have also made sure that sys.path() contains the folder path. But when I write "import hello" I get an error "ModuleNotFoundError: No module named 'hello'"
Does anyone has an idea of what is wrong is this very simple setup?
Update:
When trying to import a module that does not exist the ModuleNotFoundError is reported. After renaming the hello.dll to hello.pyd an ImportError is returned. So it seems like it tries to actually load the module.
Python compiled modules on Windows have the extension .pyd, not .dll. If you'd built it using setup.py the file would be built with the correct name. However, you built it yourself and gave it a name that Python doesn't recognise as a module.
In terms of the build command: you have to link it with libpython. You don't look to be doing this. The error you report is definitely one that you can get if the module is not linked against all its dependencies.
I know you don't want to use setup.py, however I'd use it at least once just to see what it does and if it works. You'll then at least have a command that you can copy with a working set of options.

Python import throws an OS: module not found error for vlc

So, i installed some stuff for python, and going through all my imports and trying to pip install them gives me a 'Requirement already satisfied'. When I run a script though, everytime i debug to
import vlc
I get a
OSError: [WinError 126] The specified module could not be found
directly instead of passing through to the next import. if i comment out this import, it works up to the point where I use it. I am using VSCode, my python version is set to 3.7.1, so that is not the problem. I rarely work with python, so I do not know what else I can do at this point.
While obvious to some, I did not know that VLC was Traffic Cone (tm) video player, and probably should have read the vlc docs.
You need to have the actual VLC installed on you PC for the vlc import to work with python

Specifying path to a library for a unix executable, outside of the executable

I have downloaded an unix executable 'gtselect' used for analysing scientific data. This comes as part of a larger package of tools with installation instructions such that 'gtselect' can be called from the current working directory.
When I just run 'gtselect' everything works as expected.
I then use a python script 'gt_apps.py' which uses GtApp to wrap the tools as python objects
from GtApp import GtApp
filter = GtApp('gtselect')
and then use a different script 'run.py' to call gtselect as a python object and run it:
import gt_apps
gt_apps.filter.run()
When I run this, I receive the error
dyld: Library not loaded: libdataSubselector.dylib
Referenced from: path/bin/gtselect
Reason: image not found
Now, the library it is trying to load is instead found at
path/lib/libdataSubselector.dylib
I have tried setting
export DYLD_LIBRARY_PATH = path/lib/
but the same error persists.
Any advice? Thanks

Categories