Import darknet FileNotFoundError: Could not find module - python

I am getting this error when I run import darknet:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\darknet-master\build\darknet\x64\darknet.py", line 211, in <module>
lib = CDLL(winGPUdll, RTLD_GLOBAL)
File "C:\Users\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\darknet-master\build\darknet\x64\yolo_cpp_dll.dll' (or one of its dependencies). Try using the full path with constructor syntax.```

Instead of just downloading yet another version of python (yawn), we can fix darknet.py to import the DLLs correctly. As mentioned here, we need to add correct DLL import paths if using python > 3.8. The solution is to add these lines
os.add_dll_directory('c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin')
os.add_dll_directory(os.path.dirname(__file__))
somewhere before the CDLL calls. You can place them at the top of the script.
The first line allows python to load DLLs from your CUDA install which you need if you're using GPU.
The second line allows python to load DLLs from the current working directory (into which you should copy yolo_cpp_dll.dll and pthreadVC2.dll). Alternatively you could replace this to the path that contains these DLLs.

lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)
Add the winmode=0 if python > 3.8

This is because Python 3.9 has some issue. It throws an error when the yolo_cpp_dll.dll file is imported.
Here's how to fix it:
Install Python 3.7 or lower (3.6 worked for me). You can either install version 3.7 along with your current version or remove the current one and install Python 3.7 only. If installed alongside current version then you'll have to rename the newly installed python (3.7) exe file to something like python3.7 in its installed location.
After renaming, add that directory to the PATH environment variable in Windows.
Go to your file location where darknet is stored, (for me it's C:\Users\ARYA\Documents\Penelitian1\coba1_darknet\darknet-master\build\darknet\x64\), open the Command Prompt and type python3.7 (rather than python only). This will open Python 3.7.
Here you can now run import darknet.

The answer above of #xiang zhang
worked for me by adding winmod=0 in the lines 214, 218 and 222.
if not os.path.exists(winGPUdll):
raise ValueError("NoDLL")
lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)
except (KeyError, ValueError):
hasGPU = False
if os.path.exists(winNoGPUdll):
lib = CDLL(winNoGPUdll, RTLD_GLOBAL, winmode=0)
print("Notice: CPU-only mode")
else:
# Try the other way, in case no_gpu was compile but not renamed
lib = CDLL(winGPUdll, RTLD_GLOBAL, winmode=0)

Related

Python GUI2Exe Application Standalone Build (Using Py2Exe)

I am trying to build a Python Script into a stand alone application. I am using GUI2Exe. My script uses selenium package. I have it installed.
Project compiles fine and runs on python command line directly but fails to build a stand alone because it is referring to folder:
ERROR: test_file_data_extract (__main__.FileDataExtract)
----------------------------------------------------------------------
Traceback (most recent call last):
File "File_data_extract.py", line 18, in setUp
File "selenium\webdriver\firefox\firefox_profile.pyc", line 63, in __init__
IOError: [Errno 2] No such file or directory: 'C:\\users\\username\\PycharmProjects\\Python_27_32bit\\file_data_extract\\dist\\File_data_extract.exe\\selenium\\webdriver\\firefox\\webdriver_prefs.json'
It is looking for selenium package is located at :
C:\Users\username\Anaconda2_Py27_32bit\Lib\site-packages\selenium-2.48.0-py2.7.egg\selenium\webdriver\firefox
where C:\Users\username\Anaconda2_Py27_32bit is where I installed Anaconda Python 2.7, 32 bit version. By default it is looking for in \dist\filename.exe folder.
I was able to build it using bbfreeze. It works great.
First I had to install bbfreezee via pip (one time only):
pip install bbfreeze
Create a build_package.py file as:
from bbfreeze import Freezer
f = Freezer("project_name", includes=("selenium","SendKeys",)) #list problem packages here to manually include
f.addScript("project_name_script.py")
f() # starts the freezing process
Build project:
python build_package.py bdist_bbfreezee
in folder project_name where project_name_script.py sits you find project_name_script.exe with all the include packages including selenium and sendkeys. When you distribute the package you need to distribute entire project_name because it contains all dependent library dlls (python .pyd).
More details refer official bbfreezee here:
https://pypi.python.org/pypi/bbfreeze/#downloads

Python: OSError: cannot load library libcairo.so.2

I'm having some trouble running a python script on my Windows 7 platform. I've installed Python and also cairo, last one using "pip". I'm running the script using this command:
C:\Python34>python.exe label/make_label.py
and I get the following error message:
Traceback (most recent call last):
File "label/make_label.py", line 6, in <module>
import cairocffi as cairo
File "C:\Python34\lib\site-packages\cairocffi\__init__.py", line 41, in <modul
e>
cairo = dlopen(ffi, *CAIRO_NAMES)
File "C:\Python34\lib\site-packages\cairocffi\__init__.py", line 34, in dlopen
return ffi.dlopen(names[0]) # pragma: no cover
File "C:\Python34\lib\site-packages\cffi\api.py", line 118, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
File "C:\Python34\lib\site-packages\cffi\api.py", line 411, in _make_ffi_libra
ry
backendlib = _load_backend_lib(backend, libname, flags)
File "C:\Python34\lib\site-packages\cffi\api.py", line 400, in _load_backend_l
ib
return backend.load_library(name, flags)
OSError: cannot load library libcairo.so.2: error 0x7e
What I've already done is the following:
Added the PATH to GTK/bin in the environmental variable
I check the folder GTK/bin and found "libcairo-2.dll" so I renamed it to libcairo.so
I don't know what other information could be useful in solving this but please let me know and I'll try to add it.
On Mac OS X using homebrew:
brew install cairo
brew install pango
It seems cairo depends a shared library which is not in standard search library, however, the python is calling dlopen to dynamic load the library, so you could try to put the libcairo.so.2(if it's a link, then make sure the reference locates at the same folder) in the working directory. You can also try pkg-config to set the environment. see here http://people.freedesktop.org/~dbn/pkg-config-guide.html
I just had the same issue ("OSError: cannot load library libcairo.so.2: error 0x7e"), and this is how I solved the problem on Windows (Windows 7 x64, Python 3.4.2 x86 (MSC v.1600 32 bit)):
downloaded an all-in-one bundle of the GTK+ stack including 3rd-party dependencies (which contains libcairo-2.dll and other Cairo-related libraries)
extracted this archive to a path which does NOT contain spaces (e.g. C:\Programs\gtk+)
added the extracted directory's bin subdirectory (which contains the mentioned libcairo-2.dll and other necessary files) to the PATH
Win+R, SystemPropertiesAdvanced
Environment Variables...
added this directory to the Path variable (either to the user variables or system variables, after a semicolon) (e.g. ...;C:\foo;C:\Programs\gtk+)
OK
pip install cairosvg
tested it with a very simple code, which already had worked:
import cairosvg
testsvg = '<svg height="30" width="30">\
<text y="10">123</text>\
</svg>'
svgConvertedToPng = cairosvg.svg2png(bytestring=testsvg)
print(svgConvertedToPng)
Solved on Windows 10 as follows:
download the headless UniConverter installer
Find where it installed and add its dll subdirectory to the system path.
Close and re-open the command window to get the updated path.
I just fixed this on Mac OSX 10.13 with an Anaconda Python installation and cairosvg:
$ conda install cairo pango gdk-pixbuf libffi cairosvg
$ cairosvg image.svg -o image.png
I got the idea from https://cairosvg.org/documentation/, which says that all its dependencies can be installed with WeasyPrint. WeasyPrint's documentation for installation on MacOSX at https://weasyprint.readthedocs.io/en/latest/install.html#macos says to get the dependencies from HomeBrew:
brew install python3 cairo pango gdk-pixbuf libffi
So I tried it with conda instead and it worked fine.

Python: Installation issues with pygraphviz and graphviz

I see many questions on the difficulties of properly installing pygraphviz and graphviz on Windows for Python 2.7. But no answers that I have found is solving my problem. Here's what I did:
I first installed pygraphviz using unofficial windows binaries
with this link in my anaconda (python) folder (
C:\Users\chamar\AppData\Local\Continuum\Anaconda )
Downloaded graphviz-2.36.msi and installed it under the default
path C:\Program Files (x86)\Graphviz2.36
The command import pygraphviz in Python works. But when I want to use say this function nx.graphviz_layout I get raise ValueError("Program %s not found in path."%prog)
What may cause this problem is that pygraphviz cannot locate the path of graphviz. Now, since I installed pygraphviz using the unofficial windows binary, which file can I modify to link both the library and include for graphviz's path? You would you usually find in the setup.py of pygraphviz the library and include paths when you don't use the unofficial binaries.
UPDATE 1
I added to PATH in Regedit under SOFTWARE a folder GRAPHIZ with a new key (default) with value C:\Program Files (x86)\Graphviz2.36\bin
UPDATE 2
I was having an error in the pydot.py file regarding the difficulty of Python locating the path of Graphviz. I made the changes as follow:
def _graphviz():
if os.sys.platform == 'win32':
path = r"C:/Program Files (x86)/Graphviz2.36/bin/"
progs = __find_executables(path)
return progs
find_graphviz()
{'fdp': 'C:/Program Files (x86)/Graphviz2.36/bin/fdp.exe', 'twopi': 'C:/Program Files (x86)/Graphviz2.36/bin/twopi.exe', 'neato': 'C:/Program Files (x86)/Graphviz2.36/bin/neato.exe', 'dot': 'C:/Program Files (x86)/Graphviz2.36/bin/dot.exe', 'circo': 'C:/Program Files (x86)/Graphviz2.36/bin/circo.exe'}
That seems ok with me but when I execute say:
positions = nx.graphviz_layout(G, prog='twopi', root=0)
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\networkx\drawing\nx_agraph.py", line 229, in graphviz_layout
return pygraphviz_layout(G,prog=prog,root=root,args=args)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\networkx\drawing\nx_agraph.py", line 264, in pygraphviz_layout
A.layout(prog=prog,args=args)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1305, in layout
data=self._run_prog(prog,' '.join([args,"-T",fmt]))
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1251, in _run_prog
runprog=r'"%s"'%self._get_prog(prog)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1239, in _get_prog
raise ValueError("Program %s not found in path."%prog)
ValueError: Program twopi not found in path.
Why?
Here are the steps I followed to get pygraphviz working for Python 3.4 (I think if you follow the analogous steps, it should work for Python 2.x). I'm just documenting it here for future visitors to the page :
Pre-requisites :
wheel (Should be present by default in newer distributions)
The correct Windows build of pygraphviz (unofficial builds). On Win7 x64, I selected "pygraphviz‑$version-cp34‑none‑win_amd64.whl".
(Note the cp34 indicating the python version.)
The Graphviz installer version 2.38 (for which the above wheel is built)
Steps:
Run the Graphviz installer
Add the Graphviz\bin folder to your user or system PATH
Check: Open a command prompt and execute twopi -V. You should be able to see the Graphviz version printed onto the console.
Now go to your Python environment (e.g. by running anaconda.bat, a prompt where you can run python)
Run pip install pygraphviz‑*$version*-cp34‑none‑win_amd64.whl
You're done :) ! Run an example script to see if everything went well.
You'll find loads of install-ready packages on this site: http://www.lfd.uci.edu/~gohlke/pythonlibs/ including the ones you tried to install. I know I'm way too late with the answer but I just became a member.
You may first install "easy_install" (look at
How to use Python's "easy_install" on Windows ... it's not so easy)
then 2 packages are required: 'python-pygraph' & 'libgv-python'.

Why can't this python script find the libclang dll?

I would like to get started with using libclang with Python. I am trying to get a sample code (http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/) to work on Windows, here is a part of the code I'm trying to run:
#!/usr/bin/python
# vim: set fileencoding=utf-8
import sys
import os
import clang.cindex
import itertools
...
print("Setting clang path")
# I tried multiple variations. Libclang is correctly installed in the specified location.
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin')
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin/libclang.dll')
# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")
index = clang.cindex.Index.create()
...
I stripped all the other parts of the code, but I can post them if they are relevant. The line
index = clang.cindex.Index.create()
Throws the following error:
Setting clang path
Clang path set
Traceback (most recent call last):
File "D:\libclangtest\boost_python_gen.py", line 60, in <module>
index = clang.cindex.Index.create()
File "D:\libclangtest\clang\cindex.py", line 2095, in create
return Index(conf.lib.clang_createIndex(excludeDecls, 0))
File "D:\libclangtest\clang\cindex.py", line 141, in __get__
value = self.wrapped(instance)
File "D:\libclangtest\clang\cindex.py", line 3392, in lib
lib = self.get_cindex_library()
File "D:\libclangtest\clang\cindex.py", line 3423, in get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError: [Error 193] %1 is not a valid Win32 application. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
What is the reason for this? Am I setting the dll's path wrong? I tried multiple ways, with foreslashes and backslashes, I also tried to move the dll out of Program Files to make the path contain no spaces, but nothing worked.
I am a total beginner to libclang and Python, sry if I'm asking something trivial.
I was running into a similar problem (Windows 7 x64, Anaconda3 x64). Using
import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
fixed the problem. Please note that you need to use slashes (not antislashes), and specify path to bin/libclang.dll (not to lib/libclang.dll).
#SK-logic commented that I should check whether both Python and libclang are either 32bit or 64bit. Libclang was 32bit, but I couldn't find a way to check whether my Python installation is 32 or 64, so I reinstalled the 32bit version, and now it works. So the problem probably was that I had the 64 bit version of Python.

cx_freeze fails to import psycopg2 shared lib at runtime

I have built an executable using cxfreeze (inside a python3.2 virtualenv) on my local machine.
The executable runs correctly on the local machine.
I'm trying to run the executable on a separate target machine (of identical OS and architecture), but get the following error:
...
File "/home/chris/.virtualenvs/python3env/lib/python3.2/site-packages/psycopg2/__init__.py", line 67, in <module>
File "ExtensionLoader_psycopg2__psycopg.py", line 18, in <module>
ImportError: No module named None
All the shared library dependencies are met on the target machine (according to ldd).
Based on the trace my guess is that psycopg2 is trying to load the shared library _psycopg.cpython-32mu.so (locally python3.2/site-packages/psycopg2/_psycopg.cpython-32mu.so) but can't find it at runtime.
I tried placing the library in the same directory as the executable and setting LD_LIBRARY_PATH, but neither solved the (assumed) problem.
After running strace on each process, it appears that the pure python version is looking for the file _psycopg.cpython-32mu.so
open("/home/chris/.virtualenvs/python3env/lib/python3.2/site-packages/psycopg2/_psycopg.cpython-32mu.so", O_RDONLY|O_CLOEXEC) = 8
Whereas the binary built by cxfreeze is looking for the file psycopg2._psycopg.so
open("/path/to/psycopg2._psycopg.so", O_RDONLY|O_CLOEXEC) = 3
md5sum reveals these files to be identical, so it appears that the cxfreeze process changes the expected name of the dynamic library. It's worth noting that a version of this library correctly name for the target is included in the dist directory output by cxfreeze.

Categories

Resources