Using PHP to call Virtualenv’ed Python Script

2024/7/27 9:24:27

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.

What I am trying to do:

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

What I have now:

PHP: (updated)

<?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: (updated)

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)

What the issue is:

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

What I have tried:

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

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

Yes, I have chmoded it to be executable…

I feel like this should be so simple.

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

Questions:

  • Did I set up the virtualenv wrong?
  • At the top of the Python script, instead of the “#!/usr/bin/env python3” should I call something else?
  • If so, where do I find it? Should I start from scratch and will that help?

Thank you for your help;

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

Answer

In the virtualenv'ed scripts (i.e. installed via the setuptools' entry-points), you should not touch the shebang (#!... first line). It is populated by the virtualenv & setuptools & related tools.

If you specify your own shebang, then it is not virtualenv'ed script. In that case, call python directly:

exec('/path/to/venv/bin/python3 /var/www/html/components/python/testing.py');

Alternatively, you can put the absolute path to the virtualenv's python binary to the py-script, but this does not look a good idea.

Also, remember that virtualenvs are non-relocatable. So they should stay in the path where they were created.

Also note that exec() returns only the last line of the output. You probably want shell_exec() or exec('...', $output) to get the whole output.

Also, it is unclear what happens with your script, and what is being printed on stderr. Try this command to see what is the error:

exec('/path/to/script 2>&1', $output)
#OR:
exec('/path/to/venv/bin/python3 /path/to/script 2>&1', $output)
https://en.xdnf.cn/q/73306.html

Related Q&A

How do I find my program name?

ProblemI am unable to write to a different log than the default one using syslog. I am unsure if maybe my app name is wrong in my configuration. Do "program name" and "process name"…

Is it possible to automate the execution of a Python script using Microsoft Flow?

I want to execute a snippet of python code based on some trigger using Microsoft-Flow. Is there a way to do this? Basically I am exploring on Powerapps and Microsoft-Flow. I have data in powerapp, I c…

Python: Selecting numbers with associated probabilities [duplicate]

This question already has answers here:Closed 13 years ago.Possible Duplicates:Random weighted choiceGenerate random numbers with a given (numerical) distribution I have a list of list which contains …

Weights and Biases: Login and network errors

I recently installed Weights and Biases (wandb) for recording the metrics of my machine learning projects. Everything worked fine when connected to wandb cloud instance or when I used a local docker im…

Python: Opening a file without creating a lock

Im trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I dont want my script to prevent that by locking the file; the file should be abl…

Tensorflow dataset questions about .shuffle, .batch and .repeat

I had a question about the use of batch, repeat and shuffle with tf.Dataset.It is not clear to me exactly how repeat and shuffle are used. I understand that .batch will dictate how many training exampl…

How to sort in python with multiple conditions?

I have a list with sublists as follows:result = [ [helo, 10], [bye, 50], [yeah, 5], [candy,30] ]I want to sort this with three conditions: first, by highrest integer in index 2 of sublist, then by leng…

Not able to convert Numpy array to OpenCV Mat in Cython when trying to write c++ wrapper function

I am trying to implement cv::cuda::warpPerspective in python2, there is a very sweet post about how to do that here: link. I followed the instruction as described in that post, however, I got Segmentat…

Installing python tables on mac with m1 chip

I am trying to use tables in python3 on a new mac mini with the M1 chip. I am getting multiple errors when running HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.12.0_1 pip3 install tablesERROR: Command errored …

Write unbuffered on python 3

Im trying to create a file on python without buffer, so its written at the same time I use write(). But for some reason I got an error. This is the line Im using: my_file = open("test.txt", &…