Python Heroku allow pushed .exe to run - OSError: [Errno 13] Permission denied

做~自己de王妃 提交于 2019-12-13 08:02:47

问题


I had to push an .exe file to heroku to be able to create invoice pdfs. It works localy without any problems but on heroku I get an error:

OSError: [Errno 13] Permission denied

Probably because I am not allowed to execute .exe files. So I need somehow to create a rule that this file is allowed to execute.

I pushed wkhtmltopdf.exe to heroku and I access this file in my method to create a pdf:

MYDIR = os.path.dirname(__file__)    
path_wkthmltopdf = os.path.join(MYDIR + "/static/executables/", "wkhtmltopdf.exe")
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)

Was not able to find a solution yet.

EDIT:

Tryed giving permission with chmod through heroku bash and also adding a linux executable but still the same error:

~/static/executables $ chmod a+x wkhtmltopdf-linux.exe
~ $ chmod a+x static/executables/wkhtmltopdf-linux.exe

Using sudo gave me:

bash: sudo: command not found

回答1:


I'm not very familiar with heroku, but if you can somehow get access to terminal of environment of your application (for example ssh to your server), you need to change permissions of that file so it can be executed. To do that, you need to run in that terminal:

sudo chmod a+x /path/to/file/FILENAME

Also,i'm pretty sure your app on Heroku runs on Linux, specifically on Ubuntu, since it's the default (link) It means there might be difficulties with running Windows executables.




回答2:


Okay I managed to fix this with a buildpack. In addition wkhtmltopdf-pack must be installed and added to the requirements.txt.

Then you have to set a config var in heroku for the wkhtmltopdf executable which will be generated from the files provided in the buildpack. Do not search for an .exe file.

heroku config:set WKHTMLTOPDF_BINARY=wkhtmltopdf-pack

You can see all your config vars also in the heroku dashboard under settings, you can also create it there and not use the CLI.

Then you have to tell the pdfkit configuration where to find the WKHTMLTOPDF_BINARY:

In my config.py:

import subprocess

WKHTMLTOPDF_CMD = subprocess.Popen(
['which', os.environ.get('WKHTMLTOPDF_BINARY', 'wkhtmltopdf')], # Note we default to 'wkhtmltopdf' as the binary name
stdout=subprocess.PIPE).communicate()[0].strip()

For the pdfkit configuration:

config = pdfkit.configuration(wkhtmltopdf=app.config['WKHTMLTOPDF_CMD'])

Now you should be able to create the pdf, example:

the_pdf = pdfkit.from_string("something", False, configuration=config)

Credit to this tutorial: https://artandlogic.com/2016/12/generating-pdfs-wkhtmltopdf-heroku/



来源:https://stackoverflow.com/questions/45161333/python-heroku-allow-pushed-exe-to-run-oserror-errno-13-permission-denied

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!