Python, not able to graph trees using graphviz with the anytree package - python

So I have installed the anytree package (after great effort; I had to add some environment variables on my system). Having done this I can use almost all functionalities of the anytree package - just not the one I want. I wish to use graphviz in conjunction with the anytree package in order to graph trees using the 'DotExporter' command.
I have installed graphviz; its path is C:\Users\joel\Anaconda3_2\Lib\site-packages\graphviz, and I have also added the line of code 'from anytree.exporter import DotExporter' to my Python document (in addition to the other anytree imports needed to make trees).
Therefore, having defined the root node 'root' of my tree, I should now be able to produce pngs and dot files of the tree using either of the commands 'DotExporter(root).to_picture('root.png')', however I get the following error message:
'FileNotFoundError: [WinError 2] The system cannot find the file specified'.
Does anybody know what is going on? I am using Python 3.6.2 with the engine Spyder as a part of the Anaconda distribution (I have Anaconda 3.2). I am 99% certain I have the most recent versions of anytree and graphviz.
UPDATE: So I learned that there has been a big compatibility issue between Anaconda and graphviz for about a year now and assumed that this was the problem. After trying all the solutions people suggested I still wasn't able to get my program to work so I ended up uninstalling Anaconda and instead started using PyCharm together with the standard Python interpreter. However the program still wouldn't work, so even though there are compatibility issues between Anaconda and graphviz, it turns out that wasn't the problem. I tried the program on my housemate's machine which also uses Anaconda and got the same error I had been getting.
In the end I found the following workaround: I couldn't get the 'DotExporter(root).to_picture('root.png')' command to work, but the 'DotExporter(root).to_dotfile('root.dot')' command was working. '.dot' files contain code and can be opened in Notepad (other word processing softwares will work too). Therefore I just had to find a way to graph the tree using the dot file instead of using the 'DotExporter(root).to_picture('root.png')' command. The easiest way to do this is via either of the online tools http://www.webgraphviz.com/ and http://sandbox.kidstrythisathome.com/erdos/. Just open your dot file in Notepad, copy its contents and paste it into the websites and hit generate.

Here is my workaround. I was working the examples of "getting started" with the following initial steps:
Create a tree object as udo and then trying DotExporter(udo).to_picture("udo.png"). This was where I got the same error as OP.
Assuming you have already installed graphviz and python-graphviz (e.g conda install -c anaconda graphviz python-graphviz) you can output the graph (first saving a dotfile) with this:
DotExporter(udo).to_dotfile('udo.dot')
from graphviz import Source
Source.from_file('udo.dot')
Then save image file with this:
from graphviz import render
render('dot', 'png', 'udo.dot')

Related

Problem with graphviz, ExecutableNotFound error

I have a problem with installing graphviz in PyCharm. Im using windows. I already installed graphviz library onto my PyCharm project, however error occurs when executing dot.render:
graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot')
I tried adding
import os
os.environ["PATH"] += os.pathsep + 'D:/Program Files (x86)/Graphviz2.38/bin/'
as said here: "RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38
but it still doesent work?
any ideas?
Im new to pycharm and advanced coding in general, go easy please...
This is a very common problem. While it may have several causes (see suggestions above), the solution you found is out-of-date.
Unfortunately, there are two software systems named Graphviz. You need both.
Go here https://graphviz.gitlab.io/download/ and download the appropriate version.
To check that you have installed the "real" Graphviz package, bring up a cmd window & type dot -V

Code editor not recognizing modules recognized by shell

Windows 10, Python 3.8.5, using Mu 1.0.3. I am trying to install new modules. I just started learning python this weekend so I am pretty clueless. There does not seem to be much online that addresses this problem, unless I just don't know the right words.
I have tried going to command line and entering set PYTHONPATH="[a list of all paths that came up in response to printing sys.path]". The command line seemed to accept this, since it did not give me an error message. But Mu still gives me a
ModuleNotFoundError: no module named "modulename".
I have additionally obtained just the location of the module by typing its name into the shell, and then attempting sys.path.append("path"), in both Mu and the shell. I get the same ModuleNotFoundError.
So, how do I get the code editor to find modules? And, how do I make it a permanent fix?
As per this article the Mu editor has its own Python environment, separate from the one that typical Python installations have
Due to this, to be able to use them on Windows, we have to install our requirements into a specific location:
pip install –r automate-win-requirements.txt --target "C:\Users\USERNAME\AppData\Local\Mu\pkgs"

Python: Where do I find packages like "gf" for the Anaconda Navigator and how do I install them? Python Version 3.6

this is my first question here;-)
I am using Python 3.6 via Anaconda Navigator (1.7) and that program shows a list of packages when I click Environments.
Are these packages directly useable or do I have to start any process therefor?
In a script I want to use the import-function for the package "gf", this one is not in the list above.
Where do I find it in a compatible form and how can I make it useable?
Anaconda says it has to be .yaml or .yml (Conda environment files) or .txt (Conda explicit specification files or pip requirement files)
I know the other way with the command line and WHL files, but Anaconda does not seem to have any command line as the kind-of-user-guide says.
First I tried renaming other install files to .txt, but Anaconda just took ages and got a serious problem that I had to restart it several times.
I also tried to check https://anaconda.org/conda-forge for "gf", but it seems not available there.
Best Regards
By GF, do you mean Grammatical Framework (https://www.grammaticalframework.org/)? If so, then you can't install it via Anaconda. Maybe you would be interested in one of the following options?
Jupyter kernel for GF
From https://github.com/kwarc/gf_kernel#readme:
gf_kernel is a Jupyter kernel for the Grammatical Framework (GF) . It allows you to write grammars and using them for parsing/translating/... all in one notebook, which makes it great for demos and teaching, but also for small experiments with GF.
You need to have GF installed: see https://www.grammaticalframework.org/download/index.html for options.
Using GF grammars from an external Python program
Python bindings to the PGF library are included in the GF binary, so if you have Mac or Ubuntu, the easiest way is to download the binary.
For other systems, see this blog post for how to install.

Drive issue with python NLTK

I am trying to use nltk in python, but am receiving a pop up error (windows) describing that I am missing a drive at the moment I call import nltk
Does anyone know why or how to fix this?
The error is below:
"There is no disk in the drive. Please insert a disk into drive \Device\Harddisk4\DR4."
NLTK searches for nltk_data directory until it finds one.
On Windows, these locations are scanned: %userprofile%\nltk_data, C:\nltk_data, D:\nltk_data, and so on.
Installing NLTK data or creating an empty directory solves the error.
http://www.nltk.org/data.html
My installation is Win 10, Python 3.5.2 64-bit, nltk 3.2.1 (Christoph Gohlke's binary).
While i am not sure exactly where the problem arises, I had this same error happen to me (it started 'overnight' - the code had been working, i hand not re-installed nltk, so i have no idea what caused it to start happening). I still had the problem after upgrading to the latest version of nltk (3.2.1), and re-downloading the nltk data.
shiratori's answer helped me solve my problem, although at least for me it was slightly more complicated. Specifically, my nltk data was stored in C:\Users\USERNAME\AppData\Roaming\nltk_data (i think this is a default location). This is where it had always been stored, and always had worked fine, however suddenly nltk did not seem to be recognizing this location, and hence looked in the next drive. To solve it, I copied and pasted all the data in that folder to C:\nltk_data and now it is running fine again.
Anyway, not sure if this is Windows induced problem, or what exactly changed to cause code that was working to stop working, but this solved it.
I had the same problem (Win 7, Python 3.6.5, nltk 3.3).
A simpler solution is to just define the Windows environment variable NLTK_DATA. In my case (like #kyrenia), the data is in C:\Users\USERNAME\AppData\Roaming\nltk_data.
This solution is described in the nltk 3.3 documentation: http://www.nltk.org/data.html
If you did not install the data to one of the above central locations, you will need to set the NLTK_DATA environment variable to specify the location of the data. (On a Windows machine, right click on “My Computer” then select Properties > Advanced > Environment Variables > User Variables > New...)

No module named itk (Insighttolkit) in Python

I am novice in python and Simple_ITK both to process .mha 3D image files. I have two problems:
When ever I install ITK, I always get an error as shown below:
and somehow I install ITK It doesn't work at all having no connectivity with python. And I also tried python-insighttoolkit3 package then it shows and error of
" Can't down load Python 2.8:i386" in Ubuntu software Center. I have tried various methods to install Insight Tool Kit in Ubuntu whatever I found but most of the time I get the same error as shown in picture above.
So can anybody guide me how to install and build ITK so that no error like no module named itk found.
Now another problem is I have a .py file and it has a class in which I have to fed input externally otherwise it will consider as None. SO I tried to call the file as
import /path/to/file/xxx.py
but it gives an Syntax error: invalid syntax
so how to call class from a python file from python Console .
Now, I'm using Ubuntu 15.02. For any missing details please ask.
Thanks.
1- For what I see in the image, I see that you are trying to install ITK instead of SimpleITK. For SimpleITK it is very simple using a linux package installer, like pip or easy-install. in this link you have the instructions to install it correctly. Remember to execute the instructions as super user. Maybe it is the reason of your error ( I can't see the instruction you are writing in the console ).
2- I recommend you to read the Python tutorial for modules The fastest way is to be located first on the directory of your .py file. The sequence would be:
cd /path
import module.py

Categories

Resources