6

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

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

13 Comments

I see. Since I modified the shebang, do I need to recreate the virtualenv? I don't think I moved the virtualenv script, if so, do I delete it and start over? Thanks
Either restore the original shebang, or start over.
OK I was not sure how to "restore the original shebang" so I created a new virtualenv. I created a new script inside it, it did NOT create its own shebang. I ran it fine from the command line though... with newspaper2/bin/python3 /var/www/html/components/python/test.py . Nothing from PHP. It threw sh: 1: newspaper2/bin/python3: not found Any suggestions?
Can you please add the full php & py scripts to the question? And the directory structure (venv, php&py scripts)
SPecifically, why is it newspaper2/bin/python3? Where is the leading part? Try with the absolute path.
|
1

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.

To fix this:

  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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.