How to run neato from pygraphviz on Windows - python-2.7

I am trying to use pygraphviz and networkx in python (v 2.7) to create a network map. I found a script that looks very useful on stackoverflow:
import networkx as nx
import numpy as np
import string
import pygraphviz
dt = [('len', float)]
A = np.array([(0, 0.3, 0.4, 0.7),
(0.3, 0, 0.9, 0.2),
(0.4, 0.9, 0, 0.1),
(0.7, 0.2, 0.1, 0)
])*10
A = A.view(dt)
G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),string.ascii_uppercase)))
G = nx.to_agraph(G)
G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", width="2.0")
G.draw('/tmp/out.png', format='png', prog='neato')
I get an error on the last line, basically it cannot find neato:
"ValueError: Program neato not found in path."
The error refers to the agraph.py file for pygraphviz, but I cannot see anything that could be causing the problem when I look through agraph.py
Any ideas how to resolve this? I am using windows and IDLE for my coding.
Thanks!

I had the same problem. Here's what I did in case anyone else is struggling to get pygraphvis working on Windows.
First off, I installed graphviz. I tried to install pygraphvis thrugh pip, but it refused to work. Eventually, I found the unofficial Windows binaries, so I installed that. Importing the module now works, but calling G.layout() led to the above error.
Calling neato -V worked, so it was on my PATH. I figured out that the problem was that python was running in a command prompt that was created prior to installing pygraphvis, so PATH wasn't updated. Restarting the command prompt fixed this, but led to a new error, something about C:\Program not being a valid command.
I figured that pygraphvis was probably failing to quote the path correctly, meaning it cuts off at the space in Program Files. I solved the problem by symlinking it to a path without spaces.
mklink /d C:\ProgramFilesx86 "C:\Program Files (x86)"
Note that this must be run in admin mode. You can do it by going to the start menu, typing in cmd, and then hitting Ctrl+shift+enter.
After this, I edited my PATH to refer to the symlink, restarted cmd, and everything worked.

The problem is that pygraphviz call an external program, a part of the graphviz suite called neato, to draw the graph. What is happening is that you doesn't have graphviz installed and when python try to call it it complains about not finding it. Actually pygraphviz is just a wrapper that gives you the possibility to call graphviz from inside python, but per se doesn't do anything and doesn't install graphviz by default.
The easiest solution is to try a different solution for the plot instead of neato. the accepted option are:
neato
dot
twopi
circo
fdp
nop
try one of those and see if one of them works. Otherwise you can install graphviz, that will give you the required program. It's and open-source program available on every platform, so it shouldn't be a problem to install it.
see at http://www.graphviz.org/
If you simply need to have a sketch of the graph you can use the networkx.draw function on a networkx graph, that uses matplotlib to create an interactive plot.
import networkx as nx
G = G=nx.from_numpy_matrix(A)
nx.draw(G)

Your problem is that neato is missing.
neato is part of the graphviz suite which you can install on your PC e.g. from here. (I used the .msi)
Now neato is "installed", but your system does not know where. So add the directory where neato.exe is contained in to your PATH environment variable. On Win10, this can be done with Start -> Edit environment variables for your account -> select path in the upper window -> edit -> New -> C:\Program Files (x86)\Graphviz2.38\bin\
or whatever your install directory is.

There is probably more than one cause for this error, but if it is caused by a missing path to the graphviz modules [neato,dot,twopi,circo,fdp,nop], then there is one hack that worked for me. I'm currently asking what the correct solution is, but you can use this
if not 'C:\\Program Files (x86)\\Graphviz2.38\\bin' in os.environ["PATH"]:
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'
at the beginning of your script. To generalize, if your graphviz files are saved somewhere else:
graph_path='your_bin_folder_path'
if not graph_path in os.environ["PATH"]:
os.environ["PATH"] += os.pathsep + graph_path
In particular, this worked on windows 10, using anaconda navigator and python version 3.7.

Try something like this to see where pygraphviz thinks your external programs are:
# Get paths of graphviz programs
import pygraphviz as pgv
A = pgv.AGraph()
progs_list = ['neato', 'dot', 'twopi', 'circo', 'fdp', 'nop', 'wc', 'acyclic', 'gvpr',
'gvcolor', 'ccomps', 'sccmap', 'tred', 'sfdp', 'unflatten']
for prog in progs_list:
try:
runprog = A._get_prog(prog)
print(f'{runprog}')
except ValueError as e:
print(f'{prog} gets this error: {str(e).strip()}')
After looking at the results, it's a lot of work outside your IDE installing Graphviz and setting up your Path environmental variable in the System control panel, etc.

Related

Can't build RDCOMClient using rtools40 and R 4.0

A while back, I created a fork of the RDCOMClient package to keep it working with R 3.6 (https://github.com/dkyleward/RDCOMClient). People are now running into issues again because it won't work with R 4.0. The problem doesn't seem as easy to fix, and I'm hoping for some help.
If I flip Rstudio back to R 3.6 (and rtools35), I can use the package after installing with devtools::install_github(). When I try in R 4.0 (and rtools40), the package builds and I can connect over COM to an application. The first line of code below works, and xl is a COM pointer; however, trying to do anything with it (like set Excel to visible) will crash R.
xl <- RDCOMClient::COMCreate("Excel.Application")
xl[["Visible"]] <- TRUE
Again, the above works in R 3.6.
Is there is a way to continue building with the previous rtools? I came across https://github.com/r-windows/rtools-backports#readme, which talks about using rtools35 to keep building packages, so I have hope, but I don't understand how to make it happen.
Alternatively, if there are minor changes I can make to the R or cpp code that will solve my problem, I'm all ears. I'm a cpp novice, though.
This was a quick fix :
install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
Install R-4.0.0
Install Rtools35
Edit $R_HOME/etc/x64/Makeconf (for R-4.0.0-x64)
Rcmd INSTALL RDCOMClient
Rik's answer was incredibly helpful and got a version working; however, after spending a day on it, I was able to improve on it. I want to put that here in case I have to do it again. The main improvement is being able to build a working package for both 32- and 64-bit architectures. By default, R installs both, and this makes things easier when installing dependent packages.
The first two steps are the same:
Install R-4.0.0 (https://cran.r-project.org/bin/windows/base/old/4.0.0/R-4.0.0-win.exe)
Install Rtools35 (https://cran.r-project.org/bin/windows/Rtools/Rtools35.exe) in directory c:\Rtools
If (like me) you had already installed rtools40, a system environment variable named RTOOLS40_HOME is created. The first step is to change that to:
C:\rtools
If you don't have rtools40 installed, then create the RTOOLS40_HOME system environment variable.
Two changes are still needed in the make files. These are found in your R installation directory.
In etc\x64\Makeconf, add underscores to match the rtools35 directory structure by setting these values:
MINGW_PREFIX = /mingw_$(WIN)
BINPREF ?= "$(RTOOLS40_ROOT)/mingw_64/bin/"
Do the same in etc\i386\Makeconf:
MINGW_PREFIX = /mingw_$(WIN)
BINPREF ?= "$(RTOOLS40_ROOT)/mingw_32/bin/"
Do not set BINPREF as an environment variable, or this will overwrite the makefile changes (like RTOOLS40_HOME does). With these complete, finish off with the same steps that Rik outlined:
Open windows command prompt and change to the directory that contains the RDCOMClient subdirectory and type:
R CMD INSTALL RDCOMClient –-build RDCOMClient.zip
This installs RDCOMClient in the local installation of R-4.0.0 and additionally creates the file RDCOMClient_0.94-0.zip that can be installed on other systems using the following command:
install.packages("RDCOMClient_0.94-0.zip", repos = NULL, type = "win.binary")
I can confirm that the procedure delineated in the answer above leads in the right direction but a few extra steps may be required. I can also confirm that the procedure below produces a Windows binary file that can be installed and will run under R-4.0.0:
Install R-4.0.0 (https://cran.r-project.org/bin/windows/base/old/4.0.0/R-4.0.0-win.exe)
Install Rtools35 (https://cran.r-project.org/bin/windows/Rtools/Rtools35.exe) in directory c:\Rtools
Edit $R_HOME/etc/x64/Makeconf (for R-4.0.0-x64) by changing
## The rtools40 installer sets RTOOLS40_HOME, default to standard install path
RTOOLS40_HOME ?= c:/rtools40
to
## The rtools40 installer sets RTOOLS40_HOME, default to standard install path
RTOOLS40_HOME ?= c:/rtools
Download RDCOMClient-master.zip from https://github.com/omegahat/RDCOMClient (click the green Clone button and select download zip)
Unpack to a directory named RDCOMClient
Ensure that the following PATH variables are set:
C:\Program Files\R\R-4.0.0\bin\x64 (assuming this is the location where R is installed)
C:\Rtools\bin
C:\Rtools\mingw_64\bin
Add environment variable BINPREF with the following value (the final slash is important):
C:/Rtools/mingw_64/bin/
Open windows command prompt and change to the directory that contains the RDCOMClient subdirectory and type:
R CMD INSTALL RDCOMClient –-build RDCOMClient.zip
This installs RDCOMClient in the local installation of R-4.0.0 and additionally creates the file RDCOMClient_0.94-0.zip that can be installed on other systems using the following command:
install.packages("RDCOMClient_0.94-0.zip", repos = NULL, type = "win.binary")
I am using R 4.1.2 and I found RDCOMClient will crash the R Session and the above solutions were not working.
Then, I further check with the source owner and found out the solution.
https://github.com/omegahat/RDCOMClient/issues/36
Duncantl gave the solution and it works.
dir.create("MyTemp")
remotes::install_github("BSchamberger/RDCOMClient", ref = "main", lib = "MyTemp")
If that is successful, we can then load the newly installed package with
library("RDCOMClient", lib.loc = "MyTemp")

Error with random in python 2.7

i have error running this following code in python:
from random import randint
code = "%d%d%d" % (randint(1,9), randint (1,9), randint(1,9))
guess = raw_input(">")
guesses = 0
while guess != code and guesses < 10:
print "wrong"
guesses += 1
guess = raw_input(">")
when i run it in windowpowershell i got this error"ImportError: cannot import name random". i tried to reinstall python but it still not working. Can someone guide me through this? thank you!
this is not a bug with the code. I tried running the code with python 2.7 and it runs fine. The only reason you would get this error is if, in C:>Python27>lib, there is no random.py and random. You commented that you found those files in the python27 folder, make sure that they are inside the lib folder inside of python27. Also make sure that your path for python (in edit environment variables for your account on windows 10) is the new one since you said you reinstalled it.
If you have another file with the same name random as the default module you are trying to import, Python27 will try to import that first. This can cause all sorts of issues. Try using Python3 or rename random.py to something else. Also, you should not be creating new files in C:\Python27, create a new file C:\Test\test.py like so.

Bundled executable crashes without warning when rendering plots

(I have already resolved this issue but it cost me two weeks of my time and my employer a couple of grand, so I'm sharing it here to save some poor soul.)
My company is converting our application from 32-bit to 64-bit. We create an executable using py2exe, using the bundle=2 option. The executable started crashing as soon as it tried to render a matplotlib plot.
Versions:
python==2.7.13,
matplotlib==2.0.0,
numpy==1.13.1,
py2exe==0.6.10a1
I tracked the error to the numpy library. Numpy calls numpy.linalg._umath_linalg.inv() and the program abruptly exits with no error message, warning, or traceback.
_umath_linalg is a .pyd file and I discovered that this particular .pyd file doesn't like being called from library.zip, which is where py2exe puts it when using bundle option 2 or 1.
The solution is to exclude numpy in the py2exe setup script and copy the entire package folder into the distribution directory and add that directory to the system path at the top of the main python script.

Can't find msguniq. Django 1.8, Windows 7 64 bit

I've successfully installed msguniq (can check its version), I've tried to change compatibility to windows xp and run it as administator, but still I'm getting this error:
CommandError: Can't find msguniq. Make sure you have GNU gettext tools
0.15 or newer installed.
I'm really confused because I've done everything like in this answer. I'm really out of ideas how can I fix it (also tried to change gettext.exe and xgettext.exe to windows xp compatibility).
I was having the extact same problem. And it came down to simply restart the cmd window by closing and opening it again.
Of course, you may need if not already done to install Gettext for Windows 0.15 or higher from these sources :
http://mlocati.github.io/gettext-iconv-windows/
To see how I compiled them (if you want to do it yourself) take a look at https://github.com/mlocati/gettext-iconv-windows (source)
Make sure also that the path to your gettext is in your system $PATH (python >> import sys >> print sys.path) - if not, you can add in your virtualenv folder Lib\site-packages a .pth file containing the path to your gettext folder.
Check your system path of Gettext.
Set the C:\Program Files (x86)\gettext-iconv\bin\
Not C:\Program Files (x86)\gettext-iconv\bin
Note the \ at the end.

SimpleCV 1.3 is not

I just install SimpleCV 1.3 on Windows 7. During the install I got a message: "this program could not have been correctly installed setuptools-0.6c11.win32-py2.7.exe". When I run the first example
>>> logo = Image("simplecv")
>>> logo.show()
I got an empty python window and I get a warning: "You need the python image library to save by filehandle"
Someone knows how to fix this?
thanks in advance
After the install error message, did you try reinstalling?
SimpleCV is supposed to include all the libraries it needs, so when installation failed it must have missed some libraries.
You could try installing the python image library (PIL) yourself.
I usually use this web site as a source of python libraries: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil (I think you should use Pillow-2.0.0.win32-py2.7.‌exe)