Bundle Ghostscript using Pyinstaller to python tool? - python-3.x

I am using Windows-10, Python 3.7, Pillow version 6.0.0, tkinter.
I developed a GUI tool which saves tkinter canvas as a PostScript using tkitner library, then converts it to PNG file using PIL (Pillow) library. On my Computer, I already have Ghostscript installed and my tool works just fine.
After distributing my tool with my collages, they can save the canvas as PostScript fine, but the tool then can't convert to PNG file and a anError arise regarding Ghostscript.
OSError: Unable to locate Ghostscript on paths
I would like to enable my Colleges of running the tool without the need of downloading Ghostscript on there machines.
I though then of including (bundling) the Ghostscript when creating py_app.exe using Pyinstaller.
Does anyone know how to do this, or have any other suggestions.
Below is the error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "user_input.py", line 49, in save_as
File "drawing_func.py", line 1232, in save_as_png
File "site-packages\PIL\Image.py", line 1973, in save
File "site-packages\PIL\Image.py", line 628, in _ensure_mutable
File "site-packages\PIL\Image.py", line 621, in _copy
File "site-packages\PIL\EpsImagePlugin.py", line 333, in load
File "site-packages\PIL\EpsImagePlugin.py", line 139, in Ghostscript
OSError: Unable to locate Ghostscript on paths

Related

Tree graph with python AnyTree package

I need to generate a tree from a dictionary with python AnyTree package.so I have a dictionary like below structure.
data = {'name': 'xyz',
'children': [{'name': 'node1',
'children': [{'name': 'node2'}]}]}
This dictionary can grow as the program executes.issue that I'm facing right now is when i try to export the tree as a png with DotExporter(root).to_picture("data.png") it throws a file not found error like below
Traceback (most recent call last):File "C:/Users/.../data_modeling.py", line 88, in<module>creating_tree(main)
File "C:/Users/.../data_modeling.py", line 66, in creating_tree
DotExporter(root).to_picture("data.png")
File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\site-packages\anytree\exporter\dotexporter.py", line 229, in to_picture
check_call(cmd)
File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 323, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 304, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\...\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have graphviz package installed and i am on python 3.7 with windows.
however using DotExporter(root).to_dotfile('root.dot') I can export the tree as a dot file and using an online converter I can convert dot file to an image.
but I need to export this as a png from my program.
I have already googled for similar issues and did all the suggestions and solutions there.any help or suggestion would be great,any other tree graphing tool also would be okay.
found a solution..
problem is with the graphviz python package.when you install graphviz with pip,graphviz python wrapper doesn't contain graphviz binary files.
so to solve this you need to manually download graphviz from their website and set it to path or install graphviz using conda.
alternate to this you can use pydot as a png exporter.and you can use dot file to generate a png with pydot.also pydot can be installed with pip.

Reading .doc files in python on windows 10

Note: This was flagged as a potential duplicate of this, but the point of my question is that using textract doesn't work. I am looking either for (a) a way to get textract to work on windows 10 or (b) an alternate solution.
I am building a system that needs to read various types of files. I have set up pdfminer to read the .pdfs, and based on the process outlined here I installed textract, and I can now also read .docx files. However textract relies on antiword for reading .doc files and I cannot get this to work, even after following the directions here I could not find and install a working version of antiword. I do not have microsoft word installed on my machine, and I am running windows 10 with python 3.6.5. Is there any other way to read .doc files?
Here is the bug when running textract.process('d.doc') (ignore the first error, the file is definitely there):
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 84, in run
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\ProgramData\Anaconda3\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 "<stdin>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\__init__.py", line 77, in process
return parser.process(filename, encoding, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 46, in process
byte_string = self.extract(filename, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\doc_parser.py", line 9, in extract
stdout, stderr = self.run(['antiword', filename])
File "C:\ProgramData\Anaconda3\lib\site-packages\textract\parsers\utils.py", line 91, in run
' '.join(args), 127, '', '',
textract.exceptions.ShellError: The command antiword d.doc failed with exit code 127
From the 'Windows installation problem' series: https://github.com/deanmalmgren/textract/issues/194#issuecomment-507243521
After following the steps to 'install' antiword, I was facing the same problem as yours.
Restarting windows after setting up the environment path variable entirely solved this exact error message for me. (This was the last error I was facing for handling .doc files with textract)
Installation instructions extracted from https://github.com/deanmalmgren/textract/issues/194#issuecomment-506065817
"Install Antiword
(I followed Reading .doc file in Python using antiword in Windows (also .docx))
Go to https://www.softpedia.com/get/Office-tools/Other-Office-Tools/Antiword.shtml
Extract to c:\antiword (yes it's required to be in the root directory)
Add location to path like we did tesseract-ocr [basically add c:\antiword to the system path (environment variable)]"
I was able to get part of the text using olefile, but olefile ultimately only handles bytes and does not handle the encoding of Word .doc files. The solution is to use LibreOffice, see my other question here

[ Python 2.7 ]Package program with Pynsist

I am packaging a Python 2.7 program with the lastest version of Pynsist.
I've created an installer.cfg file following this example.
But when I try to package my application running
pynsist installer.cgf
into the application folder it comes up with
Copying Python installer to build directory
PyLauncher MSI already in build directory.
Copying packages into build directory...
Traceback (most recent call last):
File "/usr/local/bin/pynsist", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/nsist/__init__.py", line 540, in main
InstallerBuilder(**args).run(makensis=(not options.no_makensis))
File "/usr/local/lib/python2.7/dist-packages/nsist/__init__.py", line 495, in run
self.prepare_packages()
File "/usr/local/lib/python2.7/dist-packages/nsist/__init__.py", line 381, in prepare_packages
py_version=self.py_version, exclude=self.exclude)
File "/usr/local/lib/python2.7/dist-packages/nsist/copymodules.py", line 224, in copy_modules
mc.copy(modname, target, exclude)
File "/usr/local/lib/python2.7/dist-packages/nsist/copymodules.py", line 195, in copy
check_package_for_ext_mods(path, self.py_version)
File "/usr/local/lib/python2.7/dist-packages/nsist/copymodules.py", line 41, in check_package_for_ext_mods
check_ext_mod(os.path.join(path, dirpath, filename), target_python)
File "/usr/local/lib/python2.7/dist-packages/nsist/copymodules.py", line 30, in check_ext_mod
raise ExtensionModuleMismatch(extensionmod_errmsg % ('Windows', path))
nsist.copymodules.ExtensionModuleMismatch: Found an extension module that will not be usable on Windows:
/usr/lib/python2.7/dist-packages/pygame/rwobject.so
Put Windows packages in pynsist_pkgs/ to avoid this.
So the problem I think is with Pygame.
On Google there in nothing about this, but i cannot use others programs for packaging(eg. py2exe, pyinstaller ecc...).
Thanks and sorry for the bad english
Reposting as an answer, since it worked:
If you put pygame in packages=, it tries to copy it from your computer. But on your computer that's pygame for Linux, which won't work on Windows. If you instead put pygame in the pypi_wheels= bit of the config file, Pynsist will take care of downloading a Windows version for you.
Have a look at the pygame example in the Pynsist repository.
Most packages don't have this problem because they only contain Python code, which is the same files on all platforms. Pygame has compiled modules, which have to be compiled for the right platform.

MoviePy Error: The system cannot find the file specified

I'm getting the error in the title when trying to run the example code below on Windows 10/Python 3.6.1/Sublime Text Editor 3. I made sure to set my MAGICK_HOME environment variable to point to where I manually installed ImageMagick. I made sure to only have 1 version of ImageMagick installed. I made sure FFMPEG, Numpy, imageio, Decorator, and tqdm were all installed.
Yes, the file "vidclip.mp4" exists. The desired behavior here is to simply clip the video and add text to the center as per the example on the github page, i.e. to simply run the code successfully.
Does anyone know what might be wrong?
Example code from github:
from moviepy.editor import *
video = VideoFileClip("vidclip.mp4").subclip(7,64)
# Make the text. Many more options are available.
txt_clip = ( TextClip("Ken Block who?",fontsize=70,color='white')
.set_position('center')
.set_duration(10) )
result = CompositeVideoClip([video, txt_clip]) # Overlay text on video
result.write_videofile("vidclip_edited.webm",fps=25) # Many options...
The full error trace:
Traceback (most recent call last):
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\video\VideoClip.py", line 1220, in __init__
subprocess_call(cmd, verbose=False )
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\tools.py", line 42, in subprocess_call
proc = sp.Popen(cmd, **popen_params)
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, 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 "C:\Users\av\Desktop\Desktop\Projects\Youtube\blender\test\testMoviePy.py", line 6, in <module>
txt_clip = ( TextClip("Ken Block who?",fontsize=70,color='white')
File "C:\Users\av\AppData\Local\Programs\Python\Python36\lib\site-packages\moviepy\video\VideoClip.py", line 1229, in __init__
raise IOError(error)
OSError: MoviePy Error: creation of None failed because of the following error:
[WinError 2] The system cannot find the file specified.
.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or.that the path you specified is incorrect
Found the problem. I didn't realize it was imperative that I specify the location of ffmpeg.exe and convert.exe within ImageMagick's config_defaults.py file. After that, I install MoviePy (in that order).

Convert IPython notebook to PDF

I have written a very long IPython notebook and need to convert it to PDF for distribution. I've read this thread but the solution didn't work for me.
I've tried the following:
If I use the Print button in my browser, I have all the IPython menu
etc. showing, which I can't accept.
If I use Print Preview from the IPython menu, none of the LaTeX equations render. They are all still just LaTeX code.
If I use ipython nbconvert --to latex --post pdf myfile.ipynb I get a FileNotFoundError. Traceback follows:
[NbConvertApp] Using existing profile dir: 'C:\\Users\\blokeley\\.ipython\\profile_default'
[NbConvertApp] Converting notebook myfile.ipynb to latex
[NbConvertApp] Support files will be in myfile_files\
[NbConvertApp] Loaded template article.tplx
[NbConvertApp] Writing 72246 bytes to myfile.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running pdflatex 3 times: ['pdflatex', 'myfile.tex']
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\ipython-script.py", line 5, in <module>
sys.exit(start_ipython())
File "C:\Anaconda3\lib\site-packages\IPython\__init__.py", line 120, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "C:\Anaconda3\lib\site-packages\IPython\config\application.py", line 564,
launch_instance
app.start()
File "C:\Anaconda3\lib\site-packages\IPython\terminal\ipapp.py", line 367, in start
return self.subapp.start()
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\nbconvertapp.py",
line 268, in start
self.convert_notebooks()
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\nbconvertapp.py",
line 311, in convert_notebooks
self.postprocessor(write_resultes)
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\base.py",
line 28, in __call__
self.postprocess(input)
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
line 140, in postprocess
cont = self.run_latex(filename)
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
line 104, in run_latex
self.latex_count, log_error)
File "C:\Anaconda3\lib\site-packages\IPython\nbconvert\postprocessors\pdf.py",
line 84, in run_command
p = subprocess.Popen(command, stdout=stdout, stdin=null)
File "C:\Anaconda3\lib\subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:\Anaconda3\lib\subprocess.py", line 1111, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
If you suspect this is an IPython bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev#scipy.org
You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
c.Application.verbose_crash=True
I'm using the latest updates from Anaconda in Python 3.4 64-bit on Windows 7 Professional.
Your issue is that you do not have LaTeX installed on your computer. When subprocess tries to call pdflatex myfile.tex it therefore fails as it cannot find pdflatex.
Whilst it's true that you can create .tex files from IPython notebooks without needing LaTeX, these are just files that contain the LaTeX code. To compile them to create PDFs you need a version of LaTeX.
Installing LaTeX on Windows is not difficult, MiKTeX is one possible installation.

Resources