Reputation: 43
I'm trying to run a python script from PHP. The python script reads the contents from a text file and creates a PDF accordingly. When I run the Python
script from terminal, it works perfectly and I get the required PDF as expected. But when I try to run the python script from PHP it gives me the following error:
Traceback (most recent call last): File "/home/amogh/server/test.py", line 3, in from fpdf import FPDF ImportError: No module named fpdf
PHP Code:
<?php
$output = shell_exec("/home/amogh/server/test.py 2>&1");
echo $output;
?>
Python code:
#! /usr/bin/python
from fpdf import FPDF
fp = open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read()
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')
I've installed fpdf
using this command:
pip install fpdf
I'm running my PHP files on lighttpd
server. Can anyone tell me where am I going wrong?
Upvotes: 1
Views: 13954
Reputation: 446
It may be because of the different users running the script. I think that when you run it from php it is executed as user www-data
Maybe you ran pip from a virtualenv or for some other reason www-data user doesn't have access to fpdf module.
Edit:
Useful links from the comments:
How to install Python Package for global use by all users (incl. www-data)
http://nocurve.com/2018/04/23/running-python-script-from-php-as-www-data/
Upvotes: 1