问题描述
我想我在我们的服务器 (Ubuntu 16.04) 上拼凑了一些脏脚本,它为我提供了一些来自 Python 的纯文本输出.
I thought I throw together a little dirty script on our server (Ubuntu 16.04) that gives me some plain text output from Python.
我想从 PHP 调用这样的脚本(我知道应该完成一些转义,但目前只是一个测试):
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;
?>
这是www-data模式774
This is script.py
owned by www-data mode 774
#!/usr/bin/python
import CoolProp.CoolProp as CP
import argparse
print('Hallo Welt')
如果我注释掉 CoolProp
导入,它会起作用.但不知何故,www-data
无法访问该包,因此脚本不返回任何内容.
If I comment out the CoolProp
import it works. But somehow the package cannot be reached by www-data
and so the script returns nothing.
如您所见,我想使用包 CoolProp
.
As you see I want to use the Package CoolProp
.
- 所以我尝试使用
pip install CoolProp
=> 安装它,这适用于我的本地用户.但是现在当从用户www-data
调用时 - 在我尝试使用目标安装它之后
--target=/usr/local/lib/site-packages/
但这没有帮助. - 我尝试将完整的
site-packages/
上的 ACL 更改为rwx
用于 www-data,但效果不佳.
- So I tried installing it with
pip install CoolProp
=> That works for my local user. But now when called from userwww-data
- After I tried to install it with a target
--target=/usr/local/lib/site-packages/
but that did not help. - I tried to change the ACL on the complete
site-packages/
torwx
for www-data but that does not work as well.
最后:pip install
一个包可以被包括www-data
在内的所有用户使用的最简单的方法是什么?
In the end: What is the simplest way to pip install
a package that can be used by all users including www-data
?
推荐答案
我建议您尝试 xotihcan 首先发布的解决方案,因为这是使最最简单的方法> 所有用户均可使用的 python 模块,包括 www-data.但是它不适用于每个 python 模块.如果这对您不起作用,或者您只想为 www-data 用户安装模块,请使用以下命令:
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
我在尝试使 Python pyro4 模块可用于 www-data 时遇到了同样的问题.还有另一种方法可以做到这一点,但它涉及一些更肮脏的黑客.有关更多详细信息,请查看我的问题/答案@如何使用 PHP 和 Apache Web 服务器正确调用 Python Pyro 客户端?
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?
这篇关于如何安装 Python 包以供所有用户使用(包括 www-data)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!