Treepoem barcode generator unable to find ghostscript - python

I'm trying to generate a pdf417 barcode in python using treepoem but pycharm keeps giving me the following error:
Traceback (most recent call last):
File "C:/Users/./Documents/barcodes.py", line 175, in
image = generate_barcode(barcode_type="pdf417",data=barcode, options=dict(eclevel=5, rows=27, columns=12))
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 141, in generate_barcode
bbox_lines = _get_bbox(code)
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 81, in _get_bbox
ghostscript = _get_ghostscript_binary()
File "C:\Users.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\treepoem__init__.py", line 108, in _get_ghostscript_binary
'Cannot determine path to ghostscript, is it installed?'
treepoem.TreepoemError: Cannot determine path to ghostscript, is it installed?
I've tried to install ghostcript, using both the .exe I found online and using pip install ghostscript (successfully completed the first time, and now tells me the requirement is satisfied), yet I still keep getting this error. Any ideas on how to fix it?

You are installing on Windows, the Windows binary differs in name from the Linux binaries and indeed differs depending whether you installed the 64 or 32-bit version.
On Linux (and MacOS) the Ghostscript binary is called 'gs', on Windows its 'gswin32' or 'gswin64' or 'gswin32c' or 'gswin64c' depending on whether you want the 32 or 64 bit version, and the command line or windowed executable.
My guess is that your script is looking for simply 'gs' and is probably expecting the path to be in the $PATH environment variable, its not clear to me what its expecting.
You could probably 'fix' this by making sure the installation path is in the $PATH environment variable and copying the executable to 'gs.exe' in that directory.
Other than that you'll need someone who can tell you what the script is looking for. Quite possibly you could just grep it.

Another solution is to edit the C:\Users\Windows.UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\treepoem__init__.py
the script is looking for gs.exe, change to gswin32.exe as shown below.
Then add the GhostScriptInstallDir\bin in the PATH in windows.
def _get_ghostscript_binary():
binary = "gswin32" # changed from 'gs' to 'gswin32'
if sys.platform.startswith("win"):
binary = EpsImagePlugin.gs_windows_binary
if not binary:
raise TreepoemError(
"Cannot determine path to ghostscript, is it installed?"
)
return binary

Related

Windows 10: PIL.Image.open(abc).load(xyz) triggers OSError: Unable to locate Ghostscript on paths

I'm using Python on Windows 10 with PyCharm. My script contains this line:
img = PIL.Image.open(io.BytesIO(ps.encode('utf-8')))
It triggers this error:
Traceback (most recent call last):
File "C:/Users/x/Desktop/ytg2/main.py", line 504, in <module>
generate_terrain(driver)
File "C:/Users/x/Desktop/ytg2/main.py", line 129, in generate_terrain
img = open_eps(ps, dpi=95.5)
File "C:/Users/x/Desktop/ytg2/main.py", line 32, in open_eps
img.load(scale=math.ceil(scale))
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 332, in load
self.im = Ghostscript(self.tile, self.size, self.fp, scale)
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 134, in Ghostscript
raise OSError("Unable to locate Ghostscript on paths")
OSError: Unable to locate Ghostscript on paths
Process finished with exit code 1
So what I understand is that the function load of the object returned by PIL.Image.open uses the package Ghostscript that can't be found with the interpreter.
So here is, in the order, what I've tried to do:
In PyCharm's packages manager, I've installed the following packages: python3-ghostscript and ghostscript.
In Windows 10 Environments Variables, I have added this variable: (name="Ghostscript" ; value="C:\Program Files\gs\gs9.52\bin\gswin64.exe"). Previously, I've of course manually installed Ghostscript (https://www.ghostscript.com/download/gsdnld.html). I 've tried this value too: %ProgramFiles%\gs%\gs9.52%\bin%\gswin64.exe.
However the problem is still here. What could I do?
The PIL.Image.open(io.BytesIO(ps.encode('utf-8'))) uses shutils.which('gswin64c') to find gswin64c (I knew that by clicking on a file link that the Python Interpreter shown in the PyCharm's console, in the error logs - this link is: one of the two last lines beginning with the word File in the error logs I've shown in the OP, if I remember well).
shutils.which('gswin64c') was returning None (indeed, I made myself a print of it) ; so I prompted os.environ["PATH"] and indeed, it was not contained in the printed output. Then to be sure, I typed echo %path% in the Windows 10 CLI, and I made the same constatation.
My conclusion was: I thought I was correctly adding the path of gswin64c in the way I mentionned in the OP of this SOflw Question (via the admin panel) but in fact, I was wrong.
(Maybe this step is optional.) So: first, since it doesn't work, I have deleted the path of gswin64c that I have added via the admin panel (cf.: the OP). This deletion was done via the admin panel too.
Then, to correctly add the path of gswin64c, I've typed, in the Windows CLI: setx path "%path%;c:\Program Files\..........\" (this path must contain gswin64c). Then I've restarted Windows 10 (if I remember well, it was required).
Then I re-printed the result of shutils.which('gswin64c') and gswin64c is found now. Also os.environ["PATH"] and echo %path% correctly output the path of gswin64c.
I hope this answer could help someone. In fact it was not very difficult: one just has to know how to correctly add a path on Windows 10.... Lol.
I was "inspired" by: https://www.windows-commandline.com/set-path-command-line/ ;-) .

'lessc' is not recognized as an internal or external command, operable program or batch file

I have been using Sublime Text 3 to compile .less css at work, but I can't get it to work on my local machine. I think it has something to do with the path variable for lessc, but I can't figure out how to find the path variable that I need to add to my path variable to make it work. Can some one help?
I'm using Windows 7 with Sublime Text 3.
Here is the error:
Writing file /C/xampp/htdocs/project/sites/project.localhost/themes/project/less/home.less with encoding UTF-8 (atomic)
[less2css] Converting C:\xampp\htdocs\project\sites\project.localhost\themes\project\less\home.less to C:\xampp\htdocs\project\sites\project.localhost\themes\project\css\home.css
error: less2css error: [WinError 2] The system cannot find the file specified
Traceback (most recent call last):
File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 549, in run_
return self.run(edit)
File "less2css in C:\Users\myMachine\AppData\Roaming\Sublime Text 3\Installed Packages\Less2Css.sublime-package", line 48, in run
File "less2css in C:\Users\myMachine\AppData\Roaming\Sublime Text 3\Installed Packages\Less2Css.sublime-package", line 15, in __init__
File "less2css in C:\Users\myMachine\AppData\Roaming\Sublime Text 3\Installed Packages\Less2Css.sublime-package", line 29, in show
File "C:\Program Files\Sublime Text 3\sublime.py", line 86, in error_message
sublime_api.error_message(msg)
TypeError: String required
Running l e s s c " . / m a i n . l e s s " " . . / c s s / m a i n . c s s " - - s o u r c e - m a p - - n o - c o l o r
This is a good question, because the lessc path is dissimilar in many ways to normal installation paths on Windows. I was able to confirm that lessc does need to be added to your Windows environment variable called PATH in order to for sublime-less2css to work properly, and this is directly from the maintainer of the sublime-less2css module: https://github.com/timdouglas/sublime-less2css
Open Control Panel
type 'env' in the Control Panel Search Bar to bring up the 'Edit the system environment variables' option (select this option, then tell the User Account Control dialog to 'Continue')
click the 'Environment Variables' button
Go to the System Variables section --> click once on the line that says PATH --> click 'Edit'
Add the following to the end of the strings in the window that comes up:
;C:\Users\{add_your_Windows_username_here}\AppData\Roaming\npm
Since my Windows user name is cknoettg, mine looks like this:
;C:\Users\cknoettg\AppData\Roaming\npm
Finally, click OK --> OK --> OK
Now, retry the program.
Important proviso: For this solution to work, you must have installed less on your Windows machine using npm - the Node.js Package Manager. (Which you can get here: http://nodejs.org/)
If you used a different method to install lessc, the exact path to add to your environment variables is going to be different than what I have suggested. Let's say you installed 'lessc' directly in a folder called "C:\less\bin". In that instance, you would add:
;C:\less\bin
to your environment variable PATH.
Also: I noticed in the error message that you posted the following line:
less2css in C:\Users\myMachine\AppData\Roaming\Sublime Text 3\Installed Packages\Less2Css.sublime-package
It is possible that your Python file is hardcoded with the path C:\Users\myMachine\AppData\Roaming\Sublime Text 3\Installed Packages\Less2Css.sublime-package. IF that is the case, it is possible that your lessc program is in the path: C:\Users{insert_your_Windows_username_here}\AppData\Roaming\Sublime Text 3\Installed Packages\ .
IF this is true, then you will not only have to modify your Windows environment variable PATH with:
;C:\Users\{insert_your_Windows_username_here}\AppData\Roaming\Sublime Text 3\Installed Packages\
You will also have to manually edit your Python file by substituting the text 'myMachine' with your current Windows username on the machine that you are located on currently. You could even try just making this direct edit to the Python code without changing the Windows environment variable, but it may or may not work.
To see a way to run your program WITHOUT editing the environment variable, you can see here (although I don't recommend it for a variety of reasons, not the least of which is the additional typing involved each time you run your script): How to install and run lessc on top of node.js and Windows?
Good luck!
Because your system cannot find the "lessc" commandline tool, you can simply install it using npm if you having node in your system:
npm install -g less
You can install less compiler by
npm install less
then you can find the compiler in this path
\node_modules\less
then copy the lessc file to where your .less file is, then run the compiler in cmd
lessc styles.less styles.css
done!
On windows you can install git-bash terminal.
Then execute npm install less. This would install it locally.
Then in the git-bash terminal navigate to your project folder and execute
./node_modules/less/bin/lessc css/styles.less css/styles.css
This Command Solved my issue:
npm install -g less

Python: Installation issues with pygraphviz and graphviz

I see many questions on the difficulties of properly installing pygraphviz and graphviz on Windows for Python 2.7. But no answers that I have found is solving my problem. Here's what I did:
I first installed pygraphviz using unofficial windows binaries
with this link in my anaconda (python) folder (
C:\Users\chamar\AppData\Local\Continuum\Anaconda )
Downloaded graphviz-2.36.msi and installed it under the default
path C:\Program Files (x86)\Graphviz2.36
The command import pygraphviz in Python works. But when I want to use say this function nx.graphviz_layout I get raise ValueError("Program %s not found in path."%prog)
What may cause this problem is that pygraphviz cannot locate the path of graphviz. Now, since I installed pygraphviz using the unofficial windows binary, which file can I modify to link both the library and include for graphviz's path? You would you usually find in the setup.py of pygraphviz the library and include paths when you don't use the unofficial binaries.
UPDATE 1
I added to PATH in Regedit under SOFTWARE a folder GRAPHIZ with a new key (default) with value C:\Program Files (x86)\Graphviz2.36\bin
UPDATE 2
I was having an error in the pydot.py file regarding the difficulty of Python locating the path of Graphviz. I made the changes as follow:
def _graphviz():
if os.sys.platform == 'win32':
path = r"C:/Program Files (x86)/Graphviz2.36/bin/"
progs = __find_executables(path)
return progs
find_graphviz()
{'fdp': 'C:/Program Files (x86)/Graphviz2.36/bin/fdp.exe', 'twopi': 'C:/Program Files (x86)/Graphviz2.36/bin/twopi.exe', 'neato': 'C:/Program Files (x86)/Graphviz2.36/bin/neato.exe', 'dot': 'C:/Program Files (x86)/Graphviz2.36/bin/dot.exe', 'circo': 'C:/Program Files (x86)/Graphviz2.36/bin/circo.exe'}
That seems ok with me but when I execute say:
positions = nx.graphviz_layout(G, prog='twopi', root=0)
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\networkx\drawing\nx_agraph.py", line 229, in graphviz_layout
return pygraphviz_layout(G,prog=prog,root=root,args=args)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\networkx\drawing\nx_agraph.py", line 264, in pygraphviz_layout
A.layout(prog=prog,args=args)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1305, in layout
data=self._run_prog(prog,' '.join([args,"-T",fmt]))
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1251, in _run_prog
runprog=r'"%s"'%self._get_prog(prog)
File "C:\Users\chamar.stu\AppData\Local\Continuum\Anaconda\lib\site-packages\pygraphviz\agraph.py", line 1239, in _get_prog
raise ValueError("Program %s not found in path."%prog)
ValueError: Program twopi not found in path.
Why?
Here are the steps I followed to get pygraphviz working for Python 3.4 (I think if you follow the analogous steps, it should work for Python 2.x). I'm just documenting it here for future visitors to the page :
Pre-requisites :
wheel (Should be present by default in newer distributions)
The correct Windows build of pygraphviz (unofficial builds). On Win7 x64, I selected "pygraphviz‑$version-cp34‑none‑win_amd64.whl".
(Note the cp34 indicating the python version.)
The Graphviz installer version 2.38 (for which the above wheel is built)
Steps:
Run the Graphviz installer
Add the Graphviz\bin folder to your user or system PATH
Check: Open a command prompt and execute twopi -V. You should be able to see the Graphviz version printed onto the console.
Now go to your Python environment (e.g. by running anaconda.bat, a prompt where you can run python)
Run pip install pygraphviz‑*$version*-cp34‑none‑win_amd64.whl
You're done :) ! Run an example script to see if everything went well.
You'll find loads of install-ready packages on this site: http://www.lfd.uci.edu/~gohlke/pythonlibs/ including the ones you tried to install. I know I'm way too late with the answer but I just became a member.
You may first install "easy_install" (look at
How to use Python's "easy_install" on Windows ... it's not so easy)
then 2 packages are required: 'python-pygraph' & 'libgv-python'.

Why can't this python script find the libclang dll?

I would like to get started with using libclang with Python. I am trying to get a sample code (http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/) to work on Windows, here is a part of the code I'm trying to run:
#!/usr/bin/python
# vim: set fileencoding=utf-8
import sys
import os
import clang.cindex
import itertools
...
print("Setting clang path")
# I tried multiple variations. Libclang is correctly installed in the specified location.
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin')
#clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin/libclang.dll')
# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")
index = clang.cindex.Index.create()
...
I stripped all the other parts of the code, but I can post them if they are relevant. The line
index = clang.cindex.Index.create()
Throws the following error:
Setting clang path
Clang path set
Traceback (most recent call last):
File "D:\libclangtest\boost_python_gen.py", line 60, in <module>
index = clang.cindex.Index.create()
File "D:\libclangtest\clang\cindex.py", line 2095, in create
return Index(conf.lib.clang_createIndex(excludeDecls, 0))
File "D:\libclangtest\clang\cindex.py", line 141, in __get__
value = self.wrapped(instance)
File "D:\libclangtest\clang\cindex.py", line 3392, in lib
lib = self.get_cindex_library()
File "D:\libclangtest\clang\cindex.py", line 3423, in get_cindex_library
raise LibclangError(msg)
clang.cindex.LibclangError: [Error 193] %1 is not a valid Win32 application. To provide a path to libclang use Config.set_library_path() or Config.set_library_file().
What is the reason for this? Am I setting the dll's path wrong? I tried multiple ways, with foreslashes and backslashes, I also tried to move the dll out of Program Files to make the path contain no spaces, but nothing worked.
I am a total beginner to libclang and Python, sry if I'm asking something trivial.
I was running into a similar problem (Windows 7 x64, Anaconda3 x64). Using
import clang.cindex
clang.cindex.Config.set_library_file('C:/Program Files/LLVM/bin/libclang.dll')
fixed the problem. Please note that you need to use slashes (not antislashes), and specify path to bin/libclang.dll (not to lib/libclang.dll).
#SK-logic commented that I should check whether both Python and libclang are either 32bit or 64bit. Libclang was 32bit, but I couldn't find a way to check whether my Python installation is 32 or 64, so I reinstalled the 32bit version, and now it works. So the problem probably was that I had the 64 bit version of Python.

Workaround read-only site-packages for cx_freeze

I'm currently trying to make cx_freeze to work on a Solaris workstation I have to work with, in order to make an executable from a Python script I have. Problem is, I'm not administrator of this machine, and installation of cx_freeze requests write to site-packages, which is read-only for me.
So, obviously, I get this error:
creating /usr/local/lib/python2.6/site-packages/cx_Freeze
error: could not create '/usr/local/lib/python2.6/site-packages/cx_Freeze': Read-only file system
And if I try to run it anyway, it fails:
bash-3.00$ python /home/xxxx/cx_freeze-4.2.3/cxfreeze --target-dir cx_dist src/p_tool.py
Traceback (most recent call last):
File "/home/xxxx/cx_freeze-4.2.3/cxfreeze", line 5, in <module>
main()
File "/home/xxxx/cx_freeze-4.2.3/cx_Freeze/main.py", line 187, in main
silent = options.silent)
File "/home/xxxx/cx_freeze-4.2.3/cx_Freeze/freezer.py", line 91, in __init__
self._VerifyConfiguration()
File "/home/xxxx/cx_freeze-4.2.3/cx_Freeze/freezer.py", line 371, in _VerifyConfiguration
self._GetInitScriptFileName()
File "/home/xxxx/cx_freeze-4.2.3/cx_Freeze/freezer.py", line 283, in _GetInitScriptFileName
raise ConfigError("no initscript named %s", name)
cx_Freeze.freezer.ConfigError: no initscript named Console
Obviously, this is linked to the failed installation. So, here's my question:
Without installation of virtualenv, could I avoid the writing to site-packages, and make cx_freeze to execute from my home folder?
EDIT I had a look at site.py documentation, and PYTHONPATH filling should be equivalent to use of site-packages. So my question is now more something like: what is the path to be added to PYTHONPATH, so that cx_freeze could be executed from any location?
Notes:
I would like to avoid to deal with virtualenv, as I'm already struggling to understand the executable tools...
I saw this question, but this still requires access to site-packages folder, plus it's not user-specific;
I tried adding the following path to PYTHONPATH, but this does not work: /home/xxxx/cx_freeze-4.2.3/build/lib.solaris-2.10-sun4v-2.6;
I'm also trying to use PyInstaller but have dependency problems (and the administrator is not really helping me).
This works like a charm for me :
$ python setup.py install --home=$HOME
Run in the source directory of cx_freeze found on the Sourceforge download page.

Categories

Resources