使用 PHP 调用 Virtualenv 的 Python 脚本 [英] Using PHP to call Virtualenv’ed Python Script

查看:42
本文介绍了使用 PHP 调用 Virtualenv 的 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我花了 5.5 个小时试图让 PHP 执行并接收 Virtualenv 的 Python 脚本的输出.没有任何效果;除了没有使用 Virtualenv 的脚本.

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.

我想做什么:

当我调用它时,我试图让 PHP 调用 Newspaper lib 输出文本的 virtualenv 安装.

I am trying to make PHP call a virtualenv’d install of the Newspaper lib output text when I call it.

我现在拥有的:

PHP:(更新)

<?php
$output = exec('newspaper2/bin/python3 /var/www/html/components/python/test.py 2>&1', $output2);
print_r(error_get_last());
echo $output2;
echo $output;

...这在使用非虚拟环境脚本时有效

…this works when using a non-virtualenv script

Python:(更新)

from newspaper import Article
url = 'http://example.com/'
article = Article(url)
article.download()
article.html
article.parse()
article.authors
article.publish_date
string = article.text
print(string)

问题是什么:

我可以从命令行运行 PHP 正在运行的脚本,它的输出很好.

I can run the script that PHP is running from the command line and it outputs just fine.

我尝试过的:

使用 PHP,(我已经尝试了所有针对 PHP 的exec"调用)它似乎无法打开虚拟环境并且什么都不返回.

With PHP, (I have tried all the "exec" calls for PHP) it cannot seem to open the virtual environment and returns nothing.

在脚本之前,我已经调用了python3"和其他一些无济于事的东西.

Before the script I have called "python3" and a few other things to no avail.

是的,我已将其修改为可执行...

Yes, I have chmoded it to be executable…

我觉得这应该很简单.

我尝试了其他帖子和整个网络上的建议,但无济于事.

I have tried suggestions on other posts and all over the web to no avail.

问题:

  • 我是否设置了 virtualenv 错误?
  • 在 Python 脚本的顶部,我应该调用其他东西而不是#!/usr/bin/env python3"?
  • 如果是这样,我在哪里可以找到它?我应该从头开始吗?帮助?

感谢您的帮助;

PS:我运行的是 Ubuntu16、PHP7,我需要使用 Python3

PS: I am running Ubuntu16, PHP7 and I need to use Python3

推荐答案

好的,我终于想通了,在这个过程中也学到了很多.我默认使用的 newspaper 库尝试写入用户主目录的基础.在这种情况下,它试图写入 www-data/var/www.

OK, I finally figured it out and learned a lot in the process. The newspaper lib that I am using by default tries to write to the base of the users home directory. In this case, it was attempting to write to www-data, /var/www.

要解决此问题:

  1. 转到 newspaper 库中的 settings.py 文件.
  2. 编辑变量 DATA_DIRECTORY = '.newspaper_scraper' 并将其更改为 DATA_DIRECTORY = '.path/to/writable/directory'
  3. 保存文件,您应该可以开始使用了.
  1. Go to the settings.py file in the newspaper library.
  2. Edit the variable DATA_DIRECTORY = '.newspaper_scraper' and change it to DATA_DIRECTORY = '.path/to/writable/directory'
  3. Save the file and you should be good to go.

我不知道为什么它没有返回可以早点解释这个问题的错误.

I have no idea why it was not returning the errors that would have explained this sooner.

希望这对其他人有帮助.

Hope this helps anyone else.

非常感谢 Sergey Vasilyev 的帮助.我非常感谢.

Thank you so much Sergey Vasilyev for your help. I appreciate it greatly.

这篇关于使用 PHP 调用 Virtualenv 的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆