7

I thought I throw together a little dirty script on our server (Ubuntu 16.04) that gives me some plain text output from Python.

I want to call the script like this from PHP (I know there should be some escaping done, but it's just a test currently):

<?php
$command = '/usr/local/bin/script.py';
$output = shell_exec($command);
echo $output;
?>

This is script.py owned by www-data mode 774

#!/usr/bin/python
import CoolProp.CoolProp as CP
import argparse
print('Hallo Welt')

If I comment out the CoolProp import it works. But somehow the package cannot be reached by www-dataand so the script returns nothing.

As you see I want to use the Package CoolProp.

  1. So I tried installing it with pip install CoolProp=> That works for my local user. But now when called from user www-data
  2. After I tried to install it with a target --target=/usr/local/lib/site-packages/ but that did not help.
  3. I tried to change the ACL on the complete site-packages/ to rwx for www-data but that does not work as well.

In the end: What is the simplest way to pip install a package that can be used by all users including www-data?

Florian
  • 83
  • 1
  • 1
  • 7
  • To add on this: Today I would do it completely different: 1) I would not call a python script from PHP but rather create a small python app (e.g. with [flask](http://flask.pocoo.org/)) and deploy it as a wsgi app 2) The latter approach perfectly supports [pipenv](https://docs.pipenv.org/) / virtual environments that bring their own package environment. – Florian Apr 07 '18 at 14:09

2 Answers2

16

I recommend that you try the solution that xotihcan posted first as that is the easy way to make most python modules available to all users including www-data. However it doesn't work for every python module. If that doesn't work for you or you just want to install modules for the www-data user only then use the following commands:

sudo mkdir /var/www/.local
sudo mkdir /var/www/.cache
sudo chown www-data.www-data /var/www/.local
sudo chown www-data.www-data /var/www/.cache
sudo -H -u www-data pip install CoolProp

I had this same issue trying to make the Python pyro4 module available for the www-data use. There is another way to do it but it involves some even dirtier hackery. For more details check out my question/answer @ How do I properly call a Python Pyro client using PHP and Apache web server?

Community
  • 1
  • 1
infinigrove
  • 489
  • 1
  • 7
  • 16
7

Run PIP with root user.

That should fix the problem.

xotihcan
  • 127
  • 1
  • 8
  • 2
    Nope, doesn't work. This might install the modules for user `root` but not anybody else. – infinigrove Jan 18 '17 at 20:16
  • 2
    @infinigrove Did you try it? Because that's not how pip works. When run as root, it installs the module in the system `site-packages` directory, making it accessible to anyone using that Python installation, unless you specify the `--user` option. – David Z Jan 19 '17 at 01:02
  • @David Z I just tried it for the CoolProp module and my bad, in this case it does work. However, in my case for pyro4 it did NOT work. Interesting. – infinigrove Jan 19 '17 at 01:39