Skip to content Skip to sidebar Skip to footer

Using Php To Call Virtualenv’ed Python Script

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

Solution 1:

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)

Solution 2:

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.

Post a Comment for "Using Php To Call Virtualenv’ed Python Script"