Python: Installation issues with pygraphviz and graphviz - python

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'.

Related

Import darknet FileNotFoundError: Could not find module

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)

Rarfile python module keeps throwing an error when trying to extract contents from a *.rar file

So im trying to simply extract the contents of a rar file into another location-
import rarfile
epath='C:\\Users\\sidharth.m\\Desktop\\Rar\\xyz.rar'
def unrar(file):
rf = rarfile.RarFile(file)
rf.extractall()
unrar(epath)
But im getting the following error -
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\rarfile.py", line 2816, in
custom_popen raise RarCannotExec("Unrar not installed?
(rarfile.UNRAR_TOOL=%r)" % UNRAR_TOOL) rarfile.RarCannotExec: Unrar
not installed? (rarfile.UNRAR_TOOL='unrar')
Ive already tried adding and installing the rarfile and unrar packages through anaconda prompt and cmd, ive also added their respective paths into the environmental path variables and if that wasnt enough i even imported them into the project folder.
Any idea why this problem could be happening?
import rarfile
rarfile_full_path='PATHTOFILE\\xyz.rar'
path_to_extract='NEW_PATH_OF_YOUR_CHOICE'
def unrar(file,path):
rf = rarfile.RarFile(file)
rf.extractall(path)
unrar(rarfile_full_path,path_to_extract)
And you need to install unrar or bsdtar on your machine; just the rarfile python package is not enough
From the API Doc:
Compressed files are extracted by executing external tool: either unrar from RARLAB or bsdtar from libarchive.
Ive already tried adding and installing the rarfile and unrar packages
through anaconda prompt and cmd
Check if you can unrar from your cmd first, outside of python

How to alter setup.py for CyIPOPT

I want to install the python package CyIpopt (https://github.com/matthias-k/cyipopt) on windows 10 and python version 3.5.2. For the binary I am using http://www.coin-or.org/Binaries/Ipopt/Ipopt-3.10.1-Win32-Win64-dll.7z (which I unpacked). The github page gives install instructions. At one point it says:
Download the source files of cyipopt and update setup.py to point to the header files and binaries of the Ipopt package, if LD_LIBRARY_PATH and pkg_config are not setup to find ipopt on their own.
I downloaded the source files from github and edited the setup.py file by altering lines 33-36 to :
IPOPT_ICLUDE_DIRS=['D:\...\Ipopt-3.10.1-Win32-Win64-dll\include\coin', np.get_include()]
IPOPT_LIBS=['IpOptFSS','IpOpt-vc10','IpOpt-vc8']
IPOPT_LIB_DIRS=['D:\...\Ipopt-3.10.1-Win32-Win64-dll\lib\\x64\Release MKL']
IPOPT_DLL=['IpOptFSS.dll','IpOpt-vc10.dll','IpOpt-vc8.dll']
The dots represent the path to these folders on my computer. Now when I run python setup.py install, it run succesfuly. However when I then run python and import ipopt I get the following error:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Linthorst\Miniconda3\lib\site-packages\ipopt-0.1.6-py3.5-win-amd64.egg\ipopt__init__.py", line 40, in
from cyipopt import *
ImportError: No module named 'cyipopt'
I suspect I altered the setup.py file incorrectly (since I made an educated guess). I am confused that it installed but doesn't work. Can anybody help me solve this error? Many thanks in advance

Unable to import git in python

I am facing these issues. Can you help me with the same ?
Why am I seeing this error ? Do I have to add anything in the requirements.txt file ?
>>> import git
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
import git
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
>>> from git import Repo
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
from git import Repo
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
I already had gitdb and smmap installed so I had to reinstall them.
You can reinstall them by running the following command in your terminal:
pip3 install --upgrade --force-reinstall gitdb; pip3 install --upgrade --force-reinstall smmap
I also got the message ImportError: 'gitdb' could not be found in your PYTHONPATH (when trying to use GitPython).BUT I had gitdb already installed!
Thanks to this hint I figured out that gitdb silently failed because it was missing smmap.
So I installed this and it worked.
You need to install gitdb package.
$ sudo easy_install gitdb
I had the same problem. However, gitdb and smmap were already installed by pip. As I used brew to install python and its dependencies on my mac, when I checked brew doctor command, it said that my /usr/local/sbin directory is not in my PATH. So I added it to my PATH (though it didn't have anything to do with the python) and everything worked out eventually.
MS Windows Versions of this problem can occur because of the order of Python versions in your system PATH, as it did for me. I did not realize that when I installed another program, it installed a newer version of Python for its own usage, and it appended my system PATH with the address to the newer version. I noticed it when I looked at the PATH variable and found two versions of Python being called. Windows uses the first it finds, and if the first doesn't match what your program expects, it gets confused and can't find the right path to the module. This is what I did to resolve it:
To check: an easy way to test if this is your problem is to see if the paths separated by semicolons are in the right order. That can be seen in the System Variables of Windows or by printing your PATH variable in your CMD shell like in this example:
C:> path
PATH=C:\Program Files (x86)\Python37-32\Scripts;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\WINDOWS
Temporary solution:
To see if it is going to fix your computer, change it in your CMD window. Your variable change will be discarded when the window is closed. One way to do this test is to copy the paths, move the Python references to the order they are needed, and write it back:
C:> set path = C:\WINDOWS;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\Program Files (x86)\Python37-32\Scripts\
Then run the Python program to see if this was your problem. Note that this is only an example; do not copy & paste it. Your path is customized for the programs on your computer.
Permanent solution: If the above test resolves your problem, you must change your System Variables to make the change permanent. For me that usually requires a reboot afterwards in order to make the variables appear in all new windows.

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.

Categories

Resources