FileNotFoundError: [Errno 2] No such file or directory: 'which' when using python pdfkit - wkhtmltopdf

I have a strange error. I try to use pdfkit to convert an HTML string to PDF, but i get a "No such file or directory: 'which'". I know that pdfkit uses wkhtmltopdf, and it tries to find this utility by using which. I have no idea what's happening. 'which' works ok and wkhtmltopdf is installed on /usr/bin/. I get the error in this line:
pdf = pdfkit.from_string(html, False, options=options)
and the traceback:
Traceback:
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/srv/zboss-git/zboss/issues/views.py" in export_comments
453. pdf = pdfkit.from_string(html, False, options=options)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/api.py" in from_string
66. configuration=configuration)
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/pdfkit.py" in __init__
38. self.configuration = (Configuration() if configuration is None
File "/srv/zboss-git/venv/lib/python3.5/site-packages/pdfkit/configuration.py" in __init__
18. ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
File "/usr/lib/python3.5/subprocess.py" in __init__
947. restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py" in _execute_child
1551. raise child_exception_type(errno_num, err_msg)
Exception Type: FileNotFoundError at /issues/export_comments/
Exception Value: [Errno 2] No such file or directory: 'which'

I found a solution.
According the documentation of python-pdfkit:
Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.
Script:
https://github.com/JazzCore/python-pdfkit/blob/master/travis/before-script.sh
I uninstall my version and run this version. I also set up manually my configuration:
config = pdfkit.configuration(wkhtmltopdf=bytes('/usr/bin/wkhtmltopdf', 'utf-8'))
pdf = pdfkit.from_string(html, False, options=options, configuration=config)
and voila!

Related

Trouble in Installing through pip3

I am trying to install packages through pip3
pip3 install keras
But, the error is as follows:
Collecting keras
Exception:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in run
wb.build(autobuilding=True)
File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 554, in _prepare_file
require_hashes
File "/usr/lib/python3/dist-packages/pip/req/req_install.py", line 278, in populate_link
self.link = finder.find_requirement(self, upgrade)
File "/usr/lib/python3/dist-packages/pip/index.py", line 465, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File "/usr/lib/python3/dist-packages/pip/index.py", line 423, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File "/usr/lib/python3/dist-packages/pip/index.py", line 568, in _get_pages
page = self._get_page(location)
File "/usr/lib/python3/dist-packages/pip/index.py", line 683, in _get_page
return HTMLPage.get_page(link, session=self.session)
File "/usr/lib/python3/dist-packages/pip/index.py", line 792, in get_page
"Cache-Control": "max-age=600",
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3/dist-packages/pip/download.py", line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/usr/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/adapters.py", line 405, in send
conn = self.get_connection(request.url, proxies)
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/adapters.py", line 303, in get_connection
proxy_manager = self.proxy_manager_for(proxy)
File "/usr/share/python-wheels/requests-2.18.4-py2.py3-none-any.whl/requests/adapters.py", line 197, in proxy_manager_for
**proxy_kwargs)
File "/usr/share/python-wheels/urllib3-1.22-py2.py3-none-any.whl/urllib3/poolmanager.py", line 440, in proxy_from_url
return ProxyManager(proxy_url=url, **kw)
File "/usr/share/python-wheels/urllib3-1.22-py2.py3-none-any.whl/urllib3/poolmanager.py", line 391, in __init__
raise ProxySchemeUnknown(proxy.scheme)
urllib3.exceptions.ProxySchemeUnknown: Not supported proxy scheme
My institute needs a proxy and I updated proxy at all places in my system using ProxyMan.
Is it due to proxy? Because I had fresh install of Linux. If because of proxy, where do i need to update proxy?
I had a similar issue and I solved it by adding my proxy settings at the end of /etc/bash.bashrc like this:
export http_proxy="http://<user>:<pass>#<proxy>:<port>
export https_proxy="https://<user>:<pass>#<proxy>:<port>
export ftp_proxy="ftp://<user>:<pass>#<proxy>:<port>
export socks_proxy="socks://<user>:<pass>#<proxy>:<port>
You can try to use easy_install + packagename instead of pip.

Hue server error "checkJobBrowserStatus" cloudera hadoop

I have some problem with accessing to Hue webUI. I just get
"500 server error"
to every acces to any web page on the Hue. The sample of the error:
From log file i got some information about type of this error
[12/Dec/2017 01:00:53 -0800] views ERROR JS ERROR: {"msg":"ReferenceError: checkJobBrowserStatus is not defined","url":"http://10.40.2.89:8888/hue/","line":1584,"column":12,"stack":"#http://10.40.2.89:8888/hue/:1584:13\nn.Callbacks/j#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:26852\nn.Callbacks/k.fireWith#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:27661\n.ready#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:29482\nI#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:29656\n"}
when i try to open other web pages i get the same error
[12/Dec/2017 01:04:56 -0800] views ERROR JS ERROR: {"msg":"ReferenceError: checkJobBrowserStatus is not defined","url":"http://10.40.2.89:8888/metastore/tables/","line":1584,"column":12,"stack":"#http://10.40.2.89:8888/metastore/tables/:1584:13\nn.Callbacks/j#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:26852\nn.Callbacks/k.fireWith#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:27661\n.ready#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:29482\nI#http://10.40.2.89:8888/static/desktop/ext/js/jquery/jquery-2.1.1.min.e40ec2161fe7.js:2:29656\n"}
and so on , similar errors on the any web page.
I've attached the full log file on Google Drive
I'm trying to open a web interface HUE with Iceweasel on Debian.
Cloudera version is CDH5 (CDH 5.13.0).
It seems like [indexer] app was blacklisted ([desktop] section in /hue/desktop/dump_config), so Hue won't work properly.
Also, looks like you are on the old UI, it is recommended to switch back to
Hue 4.
[12/Dec/2017 01:04:53 -0800] middleware INFO Processing exception: u'indexer' is not a registered namespace: Traceback (most recent call last):
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/db/transaction.py", line 371, in inner
return func(*args, **kwargs)
File "/usr/lib/hue/apps/metastore/src/metastore/views.py", line 240, in show_tables
'source_type': _get_servername(db),
File "/usr/lib/hue/desktop/core/src/desktop/lib/django_util.py", line 230, in render
**kwargs)
File "/usr/lib/hue/desktop/core/src/desktop/lib/django_util.py", line 148, in _render_to_response
return django_mako.render_to_response(template, *args, **kwargs)
File "/usr/lib/hue/desktop/core/src/desktop/lib/django_mako.py", line 125, in render_to_response
return HttpResponse(render_to_string(template_name, data_dictionary), **kwargs)
File "/usr/lib/hue/desktop/core/src/desktop/lib/django_mako.py", line 114, in render_to_string_normal
result = template.render(**data_dict)
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/template.py", line 443, in render
return runtime._render(self, self.callable_, args, data)
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 786, in _render
**_kwargs_for_callable(callable_, data))
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 818, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 844, in _exec_template
callable_(context, *args, **kwargs)
File "/tmp/tmpMMurVL/metastore/metastore.mako.py", line 142, in render_body
__M_writer(escape(unicode( assist.assistPanel() )))
File "/tmp/tmpMMurVL/metastore/assist.mako.py", line 497, in render_assistPanel
__M_writer(escape(unicode( url('indexer:importer_prefill', source_type='all', target_type='table') )))
File "/usr/lib/hue/desktop/core/src/desktop/lib/django_mako.py", line 131, in url
return reverse(view_name, args=args, kwargs=view_args)
File "/usr/lib/hue/build/env/lib/python2.7/site-packages/Django-1.6.10-py2.7.egg/django/core/urlresolvers.py", line 532, in reverse
key)
NoReverseMatch: u'indexer' is not a registered namespace
This might be a bit late, but thought of posting the solution to help others.
I was able to get this page working by setting "share_jobs" to "true" in "/etc/hue/conf.empty/hue.ini"
So the following worked for me:
[jobbrowser]
# Share submitted jobs information with all users. If set to false,
# submitted jobs are visible only to the owner and administrators.
share_jobs=true

geocoder Service error in python

I am having a service error as I am trying to run geocoders from geopy to get a location coordinates in python.I have set my proxy config, given it an api_key.
Im not sure why I am getting this error. From the search that I have done on this, it seems that this might be a proxy issue but I have that set up already.
What could be the problem here?
This is my code :
from geopy import geocoders
proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
api_key = '.......'
g = geocoders.GoogleV3(api_key=api_key,proxies=proxies, timeout=10)
location = 'Mountain View, CA'
try:
place, (lat, lng) = g.geocode(location)
except ValueError as error_message:
print("Error: geocode failed on input %s with message %s" % (location, error_message))
this is my error output:
Traceback (most recent call last):
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1254, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1106, in request
self._send_request(method, url, body, headers)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1151, in _send_request
self.endheaders(body)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1260, in connect
server_hostname=server_hostname)
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 377, in wrap_socket
_context=self)
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 752, in __init__
self.do_handshake()
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 988, in do_handshake
self._sslobj.do_handshake()
File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 633, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 143, in _call_geocoder
page = requester(req, timeout=(timeout or self.timeout), **kwargs)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 466, in open
response = self._open(req, data)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 484, in _open
'_open', req)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 444, in _call_chain
result = func(*args)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1297, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1256, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:645)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/aqm1152/Documents/TestingCode/ACERT/test_1.py", line 12, in <module>
place, (lat, lng) = g.geocode(location)
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/googlev3.py", line 217, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 171, in _call_geocoder
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: EOF occurred in violation of protocol (_ssl.c:645)
I modified your code as below, as I don't use proxy and apply an API, and it works in my machine.
from geopy import geocoders
#proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
#api_key = '.......'
g = geocoders.GoogleV3()
location = 'Mountain View, CA'
try:
place, (lat, lng) = g.geocode(location)
except ValueError as error_message:
print("Error: geocode failed on input %s with message %s" % (location, error_message))
print (place, lat, lng)
Here is the result.
Mountain View, CA, USA 37.3860517 -122.0838511
From the trackback, it looks all the errors are related to SSL. You may try to add below code at the beginning to disable SSL Certification verification as an alternative.
import ssl
# Disable SSL certificate verification
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
I have encountered similar problem in Anaconda running Python 2.7 in a env with the root running Python 3.6. The problem wasn't encountered in Anaconda with both root and env running Python 2.7 in another machine.
In addition, this SO post about proxy server set up for geopy using os module may be good reference.
Hope this help.

AttributeError: StringIO instance has no attribute 'encoding'

While using PyCharm's interactive console when debugging nosetests, I get the following error:
AttributeError: StringIO instance has no attribute 'encoding'
I found some guidance here, but I don't know how to apply it to my situation.
How can I resolve this issue?
Full Stack Trace:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1409, in do_it
result = pydevconsole.console_exec(self.thread_id, self.frame_id, self.expression, dbg)
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 475, in console_exec
need_more = exec_code(CodeFragment(expression), updated_globals, frame.f_locals, dbg)
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 392, in exec_code
interpreterInterface = get_interpreter()
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevconsole.py", line 372, in get_interpreter
interpreterInterface = InterpreterInterface(None, None, threading.currentThread())
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console.py", line 25, in __init__
self.interpreter = get_pydev_frontend(host, client_port, show_banner=show_banner)
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 488, in get_pydev_frontend
_PyDevFrontEndContainer._instance = _PyDevFrontEnd(show_banner=show_banner)
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_bundle\pydev_ipython_console_011.py", line 318, in __init__
self.ipython = PyDevTerminalInteractiveShell.instance()
File "C:\Users\jimgo\Anaconda2\lib\site-packages\traitlets\config\configurable.py", line 412, in instance
inst = cls(*args, **kwargs)
File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 396, in __init__
super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 499, in __init__
self.init_io()
File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 363, in init_io
self.enable_win_unicode_console()
File "C:\Users\jimgo\Anaconda2\lib\site-packages\IPython\terminal\interactiveshell.py", line 357, in enable_win_unicode_console
stderr=stderr_text_str)
File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\__init__.py", line 31, in enable
readline_hook.enable(use_pyreadline=use_pyreadline)
File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\readline_hook.py", line 134, in enable
check_encodings()
File "C:\Users\jimgo\Anaconda2\lib\site-packages\win_unicode_console\readline_hook.py", line 50, in check_encodings
if sys.stdin.encoding != sys.stdout.encoding:
AttributeError: StringIO instance has no attribute 'encoding'
There's an issue on the JetBrains bug tracker with a solution.
In PyCharm, go to Run > Edit Configurations > Defaults > Python tests > Nosetests. Under Additional arguments put --nocapture. Then delete all the existing test configurations from the top of the left-hand pane.
Try to debug your test again. Don't click the re-test button from the debug panel because this seems to reuse the previous configuration. Instead run the test again by right clicking on the test file for instance.
In case anyone else shows up here... I had a similar stack trace just by using:
import pdb; pdb.set_trace()
In my case it was because I had Django configured with:
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
Which was stealing my stdout.

File permission issue with apache+mod_wsgi with pyramid&pyramid_beaker

I am trying to set up a pyramid app. I am using wsgi and apache2.
I keep getting Internal server error and the contents of the apache log is
mod_wsgi (pid=11200): Exception occurred processing WSGI script '/home/ubuntu/modwsgi/env/pyramid.wsgi'.
Traceback (most recent call last):
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/pyramid-1.3-py2.6.egg/pyramid/router.py", line 191, in __call__
request._process_response_callbacks(response)
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/pyramid-1.3-py2.6.egg/pyramid/request.py", line 243, in _process_response_callbacks
callback(self, response)
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/pyramid_beaker-0.6.1-py2.6.egg/pyramid_beaker/__init__.py", line 26, in session_callback
self.persist()
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/Beaker-1.6.3-py2.6.egg/beaker/session.py", line 706, in persist
self._session().save()
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/Beaker-1.6.3-py2.6.egg/beaker/session.py", line 400, in save
**self.namespace_args)
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/Beaker-1.6.3-py2.6.egg/beaker/container.py", line 622, in __init__
util.verify_directory(self.file_dir)
File "/home/ubuntu/modwsgi/env/lib/python2.6/site-packages/Beaker-1.6.3-py2.6.egg/beaker/util.py", line 85, in verify_directory
os.makedirs(dir)
File "/usr/lib/python2.6/os.py", line 150, in makedirs
makedirs(head, mode)
File "/usr/lib/python2.6/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: 'beaker_dir'
I can use simple pserve to serve the page and run wget http://localhost:6543/user/form on the terminal. It works well with form getting downloaded. But over the browser I get the 500 error.
I am using EC2 to host the app.
In the app:main stanza I am using the following code:
session.type = file
session.data_dir = beaker_dir
session.key = mvc
session.encrypt_key = mysecretencryptionkey
session.validate_key = mysecretvalidationkey
session.cookie_on_exception = true
Thanks a lot for reading
Supply an absolute path for:
session.data_dir = beaker_dir
The current working directory could be anything, usually '/', and so path will be wrong.

Resources