I just want to draw a simple graph with Python 3 networkx & graphviz:
import networkx as nx
G = nx.complete_graph(3)
nx.draw_graphviz(G)
I'm using ubuntu14.04 and IPython3 and as usual I did pip3 install networkx and running the code gave me:
ImportError: pydot could not be loaded: http://code.google.com/p/pydot/
And I tried installing pydotplus and running the code:
/usr/local/lib/python3.4/dist-packages/networkx/drawing/nx_pydot.py in pydot_layout(G, prog, root, **kwds)
294
295 if isinstance(node,list):
--> 296 node=node[0]
297 pos=node.get_pos()[1:-1] # strip leading and trailing double quotes
298 if pos != None:
IndexError: list index out of range
and pydot2 also:
/usr/local/lib/python3.4/dist-packages/pydot.py in write(self, path, prog, format)
1893 prog = self.prog
1894
-> 1895 dot_fd = file(path, "w+b")
1896 if format == 'raw':
1897 data = self.to_string()
NameError: name 'file' is not defined
I spent quite some time searching and installing other pydots and pygraphviz combinations already but still no luck.
While this may be related:
pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible, that doesn't solve the problem in Python 3.
You can fix this by editing the line #292 from:
pydot_node = pydot.Node(make_str(n)).get_name().encode('utf-8')
to remove the encode at the end:
pydot_node = pydot.Node(make_str(n)).get_name() #.encode('utf-8')
I've reported this bug/fix here.
This seems to be the same issue as that the pydot you're using is a version incompatible with Python 3, which uses file(...). file(...) is removed in Python 3 already.
I noticed this issue and setup a Python 3 compatible version on PyPi.
For Linux systems for Python 3.x, try:
pip3 install pydot3
Or in general for Python 2.x, try:
pip install pydot3
Not a very great answer but it acts as a workaround.
First output the .dot file by
networkx.write_dot(G, 'graph.dot') using Python
and then do the appropriate graphviz output command like neato -T png graph.dot > graph.png on the command line.
Related
I m trying to use image_to_osd function of tesseract but I got this error for python 3.6, but when I test the same script in an other environment with python 3.8 it works !!, is there any configuration for python 3.6 or anything to do ?
angle_rotated_image = re.search('(?<=Rotate: )\d+',pytesseract.image_to_osd(rotated)).group(0)
error:
angle_rotated_image = re.search('(?<=Rotate: )\d+',pytesseract.image_to_osd(rotated)).group(0)
File "C:\Users\username\AppData\Roaming\Python\Python36\site-packages\pytesseract\pytesseract.py", line 543, in image_to_osd
}[output_type]()
File "C:\Users\username\AppData\Roaming\Python\Python36\site-packages\pytesseract\pytesseract.py", line 542, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\username\AppData\Roaming\Python\Python36\site-packages\pytesseract\pytesseract.py", line 287, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\username\AppData\Roaming\Python\Python36\site-packages\pytesseract\pytesseract.py", line 263, in run_tesseract
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v5.0.0.20190623 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Estimating resolution as 163 Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.')
I ran into a similar problem when trying to determine rotation of a given document and trying to use pytesseract's image_to_osd(). It was working fine for me on MacOS with tesseract 4.1.1, but it wouldn't work on Windows with tesseract 5.0.0-alpha. After reading through many threads on the topic related to the OP's error and trying various things like passing --dpi and -c min_chararacters_to_try= with no success, I finally tried using a different version of tesseract on Windows, which finally solved my problem.
Status of image_to_osd():
(PASS) OS MacOS; tesseract 4.1.1; pytesseract 0.3.0; Python 3.6.5
(PASS) OS Windows; tesseract 4.1.0; pytesseract 0.3.0; Python 3.6.5
(FAIL) OS Windows; tesseract 5.0.0; pytesseract 0.3.0; Python 3.6.5
I think pytesseract 0.3.7 will probably work too, but I didn't test it.
Note that you can still get OP's error with this, but from what I tested it's much more reasonable now, e.g., with blank pages.
For the past several weeks I have been using videowriter.write() and it has been working fine. I needed to give the program to several coworkers that have random python installations so I packaged it up in pyinstaller, which I have used successfully on other projects.
Almost everything seems to be working fine when run under pyinstaller, I can read images, capture images from the camera, show them on the screen, and write out individual images. The problem is that when I use videowriter.write() somewhere down in the bowels of the code it crashes, complaining that the images have the incorrect size. This only happens when running under pyinstaller, not when running it directly from python.
Below is a portion of the code that I extracted with a generated test image. A coworker tried to run the same example, using his copy of opencv, python, and pyinstaller and his executable seems to work. I spent the last several days trying to duplicate that setup but I don't get the same results. I have completely removed python 3 (I have a copy of 2.7 that is specifically for other tests), cleared the pip cache, cleared environment variables, and made sure all the python directories were removed prior to re-installation. I have tried python back to 3.7, different versions of opencv 4.1 through current, and all behave the same way. I have also tried using a virtual environment and setting up the core python sitting in Program Files.
The current version of opencv_videoio_ffmpeg is: opencv_videoio_ffmpeg420_64.dll.
The version that my coworker used successfully is opencv_ffmpeg410_64.dll
While it doesn't seem to make much difference, the command I have been using to build the executable is:
pyinstaller videowriter_test.py --onefile
I have also tried it without the --onefile option, it works the same but its a little harder to find the executable.
The python installation has the following installed: opencv-python, opencv-contrib-python, imutils, pynput, keyboard, virtualenv, pyinstaller. These were all installed using pip.
pyinstaller stats:
129 INFO: PyInstaller: 3.6
129 INFO: Python: 3.7.7
131 INFO: Platform: Windows-10-10.0.18362-SP0
133 INFO: wrote C:\Users\tbray\Desktop\MyProjects\Python3\Play\videowriter\videowriter_test.spec
137 INFO: UPX is not available. 139 INFO: Extending PYTHONPATH with paths
The test code:
import traceback
import cv2
import numpy as np
import imutils
# the rest are just there to make sure pyinstaller includes the same modules
# as my regular code
import copy
import platform
import pathlib
import select
import argparse
import warnings
import datetime
import json
import time
if __name__ == '__main__':
w = 640
h = 480
currentVideoLogFilename = "test.avi"
print(os.getenv('temppath'))
try:
print("Attempting to open Video Log:", currentVideoLogFilename)
video_log_file = cv2.VideoWriter(currentVideoLogFilename,
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (w, h),True)
print("Open Success")
except:
traceback.print_exc()
print("Test Image Shape:", w, h)
image = np.zeros((w, h, 3))
try:
video_log_file.write(image)
except:
traceback.print_exc()
print("Completed")
video_log_file.release()
print("file closed/released")
sys.exit(0)
If I run the using idle or pyCharm, I get this result:
================== RESTART: C:\Users\tbray\Desktop\MyProjects\Python3\Play\videowriter\videowriter_test.py ==================
None
Attempting to open Video Log: test.avi
Open Success
Test Image Shape: 640 480
Completed
file closed/released
>>>
if I run it from the virtual environment I get:
C:\Users\tbray\Desktop\MyProjects\Python3\Play\videowriter\dist>videowriter_test.exe
None
Attempting to open Video Log: test.avi
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (415) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): test.avi in function 'cv::icvExtractPattern'
Open Success
Test Image Shape: 640 480
Traceback (most recent call last):
File "videowriter_test.py", line 39, in <module>
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_mjpeg_encoder.cpp:476: error: (-215:Assertion failed) imgWidth == frameWidth && imgHeight == frameHeight && channels == 3 in function 'cv::mjpeg::MotionJpegWriter::write'
Completed
file closed/released
One interesting note is what I assume is the temp directory that is referenced in the first error message. I have no idea where the directory comes from and what happens to it, it is missing when I go looking for it.
Any suggestions?
I am trying to use the Matplotlib Basemap toolkit but am running into a FileNotFoundError related to epsg when I try importing it.
I installed it using the following command:
conda install -c conda-forge basemap
Here's my import command:
from mpl_toolkits.basemap import Basemap
Here's the error:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-d9467465a3b6> in <module>
----> 1 from mpl_toolkits.basemap import Basemap
/anaconda3/lib/python3.7/site-packages/mpl_toolkits/basemap/__init__.py in <module>
154 # create dictionary that maps epsg codes to Basemap kwargs.
155 pyproj_datadir = os.environ['PROJ_LIB']
--> 156 epsgf = open(os.path.join(pyproj_datadir,'epsg'))
157 epsg_dict={}
158 for line in epsgf:
FileNotFoundError: [Errno 2] No such file or directory: '/anaconda3/share/proj/epsg'
I'm completely new to Basemap, so I'm not sure how to go about fixing this. I'm using Jupyter Notebook to run Python 3.7.3 on macOS Mojave 10.14.6.
Thanks in advance.
You are looking for this file: https://github.com/matplotlib/basemap/blob/master/lib/mpl_toolkits/basemap/data/epsg
Your PROJ_LIB env var does not appear to end with .../site-packages.
Please refer to https://ctcoding.wordpress.com/2019/01/29/solved-proj_lib-error-when-installing-basemap-on-windows-using-anaconda/
Take a look at $ conda info --base, and then run
$ find `conda info --base` -name epsg
to verify the file was installed in the intended directory.
Adjust PROJ_LIB to point there.
As said in another comment run this command in terminal or in the Anaconda executor thing (if you run it in Anaconda make sure you redo the single quotes part if you copy and paste the below):
$ find `conda info --base` -name epsg
When you run that command several file paths should come up. Before you import Basemap into your program put the following lines:
import os
os.environ['PROJ_LIB'] = '/Users/XXXXXXXX/opt/anaconda3/pkgs/proj4-5.2.0-h0a44026_1/share/proj/'
where the whole line in quotes is the filepath that appears from the first line of code (without the epsg/ at the end). The part in quotes above is what appeared for me. If no file paths come up or another issue arrises check out the github pages on this (just google the error) or try
conda install -c conda-forge proj4
I'm compiling windows installers for my python code. I mostly write language-related tools and include examples that require utf-8 strings in my documentation, including the README file.
I'm slowly moving from Python 2 to Python 3 and recently found that the command
python setup.py bdist_wininst
works fine with python 2 but not for python 3.
I've traced the problem to the inclusion of unicode in my readme file. The readme file gets read into setup.py.
The bug occurs in Python36\lib\distutils\command\bdist_wininst.py
The error is:
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character
In python 2.7 the relevant code in bdist_wininst.py is
if isinstance(cfgdata, str):
cfgdata = cfgdata.encode("mbcs")
In python 3.6 the equivalent code in bdist_wininst.py is
try:
unicode
except NameError:
pass
else:
if isinstance(cfgdata, unicode):
cfgdata = cfgdata.encode("mbcs")
Here is my readme file:
https://github.com/timmahrt/pysle/blob/master/README.rst
And here is my setup.py file that reads in the README file
https://github.com/timmahrt/pysle/blob/master/setup.py
And the relevant line from setup.py:
long_description=codecs.open('README.rst', 'r', encoding="utf-8").read()
My question:
Is there a way to make python 3 happy in this case?
I try to do a basic igraph plotting in an IPython notebook:
fn = 'misrables.gml'
gr = igraph.Graph.Read_GML(fn)
igraph.plot(gr)
instead of seeing a graph I see this:
Out[7]: <igraph.drawing.Plot at 0x1120d6978>
How do I persuade IPython to actually show the graphics?
Some clarifications
Inline matplotlib works fine, so this is not the issue.
I have cairo installed:
import cairo
cairo.version
output: 1.10.0
Calling _repr_svg() on the plot object results in an error:
TypeError Traceback (most recent call last)
/Users/boris/Documents/workspace/communityAnalysis/src/utils.py in <module>()
----> 1 z = ig.plot(gr)._repr_svg_()
/usr/local/lib/python3.4/site-packages/igraph/drawing/__init__.py in _repr_svg_(self)
362 # No idea why this is needed but python crashes without
363 context.show_page()
--> 364 surface.finish()
365 # Return the raw SVG representation
366 return io.getvalue()
TypeError: string argument expected, got 'bytes'
Do you have the Cairo library and its Python bindings installed on your machine? The Plot object of igraph has a _repr_svg_ method that should be enough for IPython to show the plot, but _repr_svg_ uses Cairo behind the scenes to draw the graph into an SVG file, so if you don't have Cairo, _repr_svg_ probably throws an exception that IPython swallows, and then you get an ordinary repr() representation for the plot.
Update: based on the updated post, it turned out that the problem was that _repr_svg_ was not prepared for Python 3.x as it used a StringIO object where a BytesIO object would have been appropriate. Replacing io = StringIO() with io = BytesIO() at the beginning of the function and replacing return io.getvalue() with return io.getvalue().decode() at the end solves the problem. (from io import BytesIO is also needed at the top of igraph/drawing/__init__.py). A bug report has been filed on GitHub; the issue will be fixed in the next minor release.
Update: the issue is now fixed in the development version; see this commit for a patch.
Update for igraph 0.7.1 in python 3.7.4 and IPython 7.8.0
if you install the package with:
conda install python-igraph
Then you can simply use
igraph.plot(gr).show()
and in Ubuntu a .png will be displayed automatically in a separate window with ImageMagick