Installing Graphviz for use with Python 3 on Windows 10 - python

So I've had Python 3.6 on my Windows 10 computer for a while now, and today I just downloaded and installed the graphviz 0.8.2 (https://pypi.python.org/pypi/graphviz) package via the admin commandline with:
pip3 install graphviz
It was only after this point that I downloaded the Graphviz 2.38 MSI installer file and installed the program at:
C:\Program Files (x86)\Graphviz2.38
So then I tried to run this simple Python program:
from graphviz import Digraph
dot = Digraph(comment="The round table")
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.render('round-table.gv', view=True)
But unfortunately, I received the following error when I try to run my Python program from commandline:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\graphviz\backend.py", line 124, in render
subprocess.check_call(args, startupinfo=STARTUPINFO, stderr=stderr)
File "C:\Program Files\Python36\lib\subprocess.py", line 286, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Program Files\Python36\lib\subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Program Files\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\foldername\testing.py", line 11, in <module>
dot.render('round-table.gv', view=True)
File "C:\Program Files\Python36\lib\site-packages\graphviz\files.py", line 176, in render
rendered = backend.render(self._engine, self._format, filepath)
File "C:\Program Files\Python36\lib\site-packages\graphviz\backend.py", line 127, in render
raise ExecutableNotFound(args)
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'round-table.gv'], make sure the Graphviz executables are on your systems' PATH
Notice how what I've asked seems VERY similar to this question asked here:
"RuntimeError: Make sure the Graphviz executables are on your system's path" after installing Graphviz 2.38
But for some reason, adding those paths (suggested in the solutions at the link above) to the system variables isn't working, and I don't know why! I tried restarting the computer after adding the paths as well, still to no success. See the image below:
Although the other suggested solution, which was to add these few lines in front of my Python code, did work:
import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
But here's the issue: I don't understand why adding to the environment variables didn't work, and this is my primary concern. So my question is this: why did adding those lines of code in front of the Python script work but changing the environment variables didn't? What do I need to do to get my script to run without adding those lines of code in front?

Can you please post the output you get when you type SET in a cmd window after setting the PATH environment variable?
Does it contain C:/Program Files (x86)/Graphviz2.38/bin/ ?
A cmd window must be restarted before updated environment variables become effective!

Related

Is it possible to use Tabula-Py on Portable IDE

I am new to python and am working on setting up some automation for my job in python and part of that is pulling data from tables in pdf files. Short version is that no matter how I try and what I have looked up I cannot get Tabula-Py to look at the path to java on my portable drive.
I am using a portable IDE set-up since I do not have admin privilege's on my work computer.
Tabula-Py throws the usual cannot find Java make sure it is in your PATH error message. I am using Python Portable and jPortable installed to a common directory with Spyder portable as the IDE. I have run pip install and uninstall on both Tabula and Tabula-Py multiple times. I have also run import sys for sys.path.append to add the filepath to my Java bin.
Code:
import pandas as pd
import numpy
import tabula
import sys
sys.path.append('E:\CommonFiles\Java\bin')
df = tabula.read_pdf('E:\CommonFiles\Python-Portable-3.9.6\Scripts\Sample.pdf', pages='all')
Error Message:
runfile('E:/CommonFiles/Python-Portable-3.9.6/Scripts/untitled01.py', wdir='E:/CommonFiles/Python-Portable-3.9.6/Scripts')
Traceback (most recent call last):
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\site-packages\tabula\io.py", line 80, in _run
result = subprocess.run(
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\CommonFiles\Python-Portable-3.9.6\Scripts\untitled01.py", line 15, in <module>
df = tabula.read_pdf('E:\CommonFiles\Python-Portable-3.9.6\Scripts\Sample.pdf', pages='all')
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\site-packages\tabula\io.py", line 322, in read_pdf
output = _run(java_options, kwargs, path, encoding)
File "E:\CommonFiles\Python-Portable-3.9.6\apps\lib\site-packages\tabula\io.py", line 91, in _run
raise JavaNotFoundError(JAVA_NOT_FOUND_ERROR)
JavaNotFoundError: `java` command is not found from this Python process.Please ensure Java is installed and PATH is set for `java`
I have also attempted to use camelot with a similar frustration over the ghostscript.dll.
Finally I looked into pdfplumber but had even less luck there getting it to find the tables let alone do anything with them.
I am sure this is doable but my google-fu is failing me currently and have spent the better part of 3 days looking into this with no solution I could find through Google, StackOverflow, Reddit, etc.
I had the same issue, and the solution I found is by using portable Java and registering it in the user environment path.
This explains how to install java from the EXE installer https://stackoverflow.com/a/6571736/11322275
Then, register where you saved the java folder to the user environment path as explained here https://stackoverflow.com/a/67844469/11322275
Make sure you can call java -version on your command prompt once you've done the above

Adding pip libraries in pyinstaller to create an exe file for my script

I am able to run my exe file but my scripts has multiple pip libraries for it to work. I always get this error when running my script.
Exception in thread Thread-1:
Traceback (most recent call last):
File "threading.py", line 932, in _bootstrap_inner
File "threading.py", line 870, in run
File "AIO_Bot.py", line 265, in main_function
File "cloudscraper/__init__.py", line 665, in create_scraper
File "cloudscraper/__init__.py", line 120, in __init__
File "cloudscraper/user_agent/__init__.py", line 20, in __init__
File "cloudscraper/user_agent/__init__.py", line 77, in loadUserAgent
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/8g/gkf53znx7_7405c26w344ftw0000gn/T/_MEIujM2ll/cloudscraper/user_agent/browsers.json'
Finished in 0.0 second(s)
If anyone knows how to fix this please help!
It seems that your browswers.json is expected to be found in a relative path from the "exe" (cloudscraper/user_agent) when it is not the case when it is really a exe file. You have to modify your pyinstaller build command to include that file in the package. Add
--add-data '/lib/python3.8/site-packages/cloudscraper/user_agent/browsers.json:cloudscraper/user_agent' and it should work. (replace : with ; on Windows)
You can refer to https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files for more details.
It seems your script/exe is looking for a file named "browsers.json".
Locate it and try putting it in the same folder as your created *.exe
The file is not being located because you are searching through a path that can not be opened. If your script is on the same level of the path then you would only have to do this
(browsers.json)
to fix this just exclude the directory path
mv /lib/python3.8/site-packages/cloudscraper/user_agent/browsers.json /var/folders/8g/gkf53znx7_7405c26w344ftw0000gn/T/_MEIujM2ll/cloudscraper/user_agent/
if this does not work pip install cloudscraper in terminal instead because pycharms assigniing the file to it's own enviroment

mlabwrap setup Mac OS X

I have been trying to install the mlabwrap Python library, but keep on running into the following error when I run the setup.py file using the command python setup.py install in the terminal:
Traceback (most recent call last):
File "setup.py", line 130, in <module>
queried_version, queried_dir, queried_platform_dir = matlab_params(cmd, WINDOWS, extra_args)
File "setup.py", line 93, in matlab_params
error = call(cmd, **extra_args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I have done some investigation, and found the following forum post: https://sourceforge.net/p/mlabwrap/mailman/message/29077359/ to resolve the issue. Following the advice in this thread, I changed the following things in the setup.py file:
__version__ = '7.3'
and
MATLAB_DIR = '/Applications/MATLAB_R2015a.app'
I have a feeling that the problem is related to finding the directory that matlab is located in, but I seem to be unable to resolve this. I didn't change my $PATH variable because I was a little confused by the advice given in the thread. Any insight would be much appreciated!
In order to install mlabwrap, you must modify the setup.py file to:
MATLAB_COMMAND = '/Applications/MATLAB_R2015a.app/bin/matlab' # specify a full path if not in PATH
MATLAB_VERSION = 7.3 # e.g: 6 (one of (6, 6.5, 7, 7.3))
# 7.3 includes later versions as well
MATLAB_DIR= '/Applications/MATLAB_R2015a.app/' # e.g: '/usr/local/matlab'; 'c:/matlab6'
PLATFORM_DIR='maci64' # e.g: 'glnx86'; r'win32/microsoft/msvc60'
EXTRA_COMPILE_ARGS=None # e.g: ['-G']
The parameters may change depending on what version of MATLAB you are using and what operating system you are using.

Spyder won't connect to kernel / iPython console

Since my laptop crashed (for unknown reason) last night, Spyder has not been able to connect to a kernel. I am getting this error:
IOError: Could not find u'kernel-1809.json' in ['.', '~/Library/Jupyter/runtime']
(The name of the json file changes each time I attempt to launch an iPython console.)
This does not seem to be related to the matplotlib 1.5 issue, that I have seen elsewhere on here - it is using 1.4.3.
iPython (version 4.0.0) runs fine from the command line.
Mac 10.10.1 (Yosemite). Spyder 2.3.7. Python 2.7.10 64bits, Qt 4.8.7, PyQt4 (API v2) 4.11.3 on Darwin
UPDATED TO INCLUDE LOGS
>>> /Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/nbformat/current.py:19: UserWarning: nbformat.current is deprecated.
- use nbformat for read/write/validate public API
- use nbformat.vX directly to composing notebooks of a particular version
ERROR: 3rd party plugin import failed for `p_pylint`
Traceback (most recent call last):
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/otherplugins.py", line 53, in get_spyderplugins_mods
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderplugins/p_pylint.py", line 23, in <module>
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderplugins/widgets/pylintgui.py", line 71, in <module>
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderplugins/widgets/pylintgui.py", line 61, in get_pylint_version
File "subprocess.pyc", line 710, in __init__
File "subprocess.pyc", line 1335, in _execute_child
OSError: [Errno 2] No such file or directory
Traceback (most recent call last):
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/plugins/externalconsole.py", line 925, in <lambda>
give_focus=give_ipyclient_focus))
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/plugins/externalconsole.py", line 1033, in register_ipyclient
give_focus=give_focus)
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/plugins/ipythonconsole.py", line 836, in register_client
self.connect_client_to_kernel(client)
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/plugins/ipythonconsole.py", line 1048, in connect_client_to_kernel
client.password)
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/plugins/ipythonconsole.py", line 1017, in create_kernel_manager_and_client
cf = find_connection_file(connection_file)
File "/Volumes/Spyder-Py2/Spyder-Py2.app/Contents/Resources/lib/python2.7/jupyter_client/connect.py", line 185, in find_connection_file
raise IOError("Could not find %r in %r" % (filename, path))
IOError: Could not find u'kernel-2018.json' in ['.', u'/Users/garyspatterson/Library/Jupyter/runtime']
** Further update **
I deleted all files under the .spyder2/ folder in my user directory, reopened Spyder, and it was able to connect to an iPython console. So far, so good. Trouble is, when I added a path to the PYTHONPATH manager (so I could import external modules, such as nltk), I am back to getting the same error.
Had a similar issue and was able to resolve it by creating a new virtualenv with the same dependencies installed.
Now Spyder is working with the PYTHONPATH pointing at lib/python2.7/site-packages of the new environment.

how to make pytesser (Tesseract) work?

I'm trying to make pytesser (downloadable here) work on my mac OS, but I don't succeed.
I installed Tesseract, PIL and all the dependencies.
I unzipped pytesser in my python lib folder and modified the script file into __init__.py
in the init file I modified the path to the tesseract.exe file as suggested here and here
that is:
tesseract_exe_name = 'my lib path/pytesser/tesseract' # Name of executable to be called at command line
that's what I get as error:
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
print image_to_string(picz)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pytesser/__init__.py", line 31, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pytesser/__init__.py", line 21, in call_tesseract
proc = subprocess.Popen(args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
it seems that the module does not manage to run the .exe file. I tried to change the path, add the extension .exe but I always get the same error.
Several solutions for a python tesseract wrapper:
Python-Tesseract:
First of get homebrew and brew install python, then easy_install https://bitbucket.org/3togo/python-tesseract/downloads/python_tesseract-0.9.1-py2.7-macosx-10.10-x86_64.egg
source: https://code.google.com/p/python-tesseract/wiki/HowToCompileForHomebrewMac
pytesseract:
This what I was using previously before getting python-tesseract, pip install pytesseract. Then you have to go to /usr/local/lib/python2.7/site-packages and go to pytesseract then pytesseract.py. Change the file path in the python script to where tesseract is located on your computer.

Categories

Resources