title: Executing a Python script with PHP using Apache server
body: <p>I'm experiencing an odd issue. I have a Python script that I'm calling from a PHP script. This is all running on an Apache server on Ubuntu 18.04. Part of the Python script uses the Google Drive API. EDIT: See the bottom <strike>After a lot of testing and replication,I've concluded that simply having the following Google Drive Python libraries and dependencies imported:</strike></p>
  
  <pre><code>from __future__ import print_function
  from googleapiclient import discovery
  from httplib2 import Http
  from oauth2client import file, client, tools
  from googleapiclient.http import MediaIoBaseDownload
  </code></pre>
  
  <p>Messes up the script when it is called from the web. By that I mean the Python script does not seem to execute, and any shell output that I should be getting through print statements do not make their way back to the PHP script. When calling the script locally (python myscript.py), it works just fine.</p>
  
  <p>The weird part is that when I remove these import statements from the Python script, it executes just fine from both PHP, and from directly launching the script from a browser. In both of those cases I am also able to get the shell output back to the PHP script. I have given the proper permissions for the Python script, and I have configured Apache to be able run CGI scripts. Here is what my Python script looks like:</p>
  
  <pre><code>#!/usr/bin/env python3
  import cgitb
  
  from __future__ import print_function
  from googleapiclient import discovery
  from httplib2 import Http
  from oauth2client import file, client, tools
  from googleapiclient.http import MediaIoBaseDownload
  
  cgitb.enable()
  print("Hello World")
  </code></pre>
  
  <p>And here is what my PHP script looks like:</p>
  
  <pre><code>&lt;?php
      ini_set('display_errors', 1);
      ini_set('display_startup_errors', 1);
      error_reporting(E_ALL);
  
      $output = shell_exec("python path_to_my_script/myscript.py");
      echo $output;
  ?&gt;
  </code></pre>
  
  <p>EDIT:
  I ran a couple of tests using another 3rd party Python library that I installed with pip, and this actually seems to be an issue with importing any 3rd party library when executing the script via PHP.</p>