Executing PHP code that imports a Python library

My attempt to execute a Python script from a PHP file is encountering issues when it comes to loading a local library. Surprisingly, the PHP successfully calls the Python script without the local libraries, and manually launching the Python script works flawlessly.

Below are the minimal files that (do not) work:

index.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    <?php
      $command = escapeshellcmd("./getter.py");
      $output = shell_exec($command);
      echo "<p>$output</p>";
    ?>    
  </body>
</html>

getter.py

#!/usr/bin/env python2
import got
if __name__ == "__main__":
  print "Python2 working"

The got library is causing the issue.

Manually executing ./getter.py leads to successful printing, but the webpage remains blank. If I comment out import got, the message appears on the webpage as well.

Further details:

  • Operating System: Ubuntu 18.04.1
  • Web Server: Nginx 1.14.0
  • PHP Version: 7.2
  • Python Version: 2.7.15rc1
  • I am new to web development and yet to explore debugging tools.
  • No issues occur with non-local library imports like os and csv.
  • Tried using a simpler command to run the Python script -
    $output = shell_exec("./getter.py");
  • Attempted changing #!/usr/bin/env python2 to #!/usr/bin/python2.
  • Both PHP and Python confirm they are in the directory /var/html/www/test.

Folder structure:

├── getter.py
├── got
│   ├── __init__.py
│   ├── manager
│   │   ├── __init__.py
│   │   ├── TweetCriteria.py
│   │   └── TweetManager.py
│   └── models
│       ├── __init__.py
│       └── Tweet.py
├── index.php
├── __init__.py
└── lib
    ├── __init__.py
    └── toto.py

Thank you for any assistance provided.

Answer №1

The issue stemmed from a dependency problem where certain essential libraries were not installed for all users. This resulted in Nginx being unable to access them as root, although regular users could.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Netbeans users may experience issues with xdebug where debugged lines are not properly highlighted

After successfully setting up xdebug, I am encountering an unusual issue. While I am able to view the variables and step through the code, I am unable to identify the specific line that I am debugging. Are there any suggestions on how to resolve this? ...

Iterating through a list in Python to check if an item exists in a line

I need to iterate through two lists and only print an item if it is present in the second list. However, I am working with very large files and do not want to load them into memory as a list or dictionary. Is there a method to achieve this without storing ...

Is there a way to include input text along with a file upload in an AJAX request to upload.php? And how exactly would I go

How do I include the book name entered in the form field with id:bname in the request to be sent to the page upload.php, and how can I retrieve this text on the upload.php page? function uploadFile(){ var file = document.getElementById("upload"). ...

Discover the solution by utilizing XPath

I am struggling to extract data from an HTML table: <div class="parameters"> <div class="property">property 1</div> <div class="value">value</div> </div> <div class="paramete ...

Failed to generate a seamless transition between the files

I've been attempting to generate a gradient effect by utilizing two files and an import statement, but I'm consistently encountering an error. Let's take a look at the files. backrounds.py # Keigen Sheppard, CS 115, Autumn 2021 # Programmin ...

Leveraging axes_grid1 without altering the subplots

I need to add a colorbar next to one of my subplots using axes_grid1. However, when I use the make_axes_locatable function, it alters the size of my subplot compared to the other plots. Below is a simple demonstration showcasing my problem: from mpl_tool ...

What causes the delay in processing YAML compared to JSON when dealing with the same or larger data sets?

Just a couple of hours ago, I executed this code: python -c "import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)" < input.yaml > output.json I'm dealing with a massive 450 MB YAML file and the amount of time it's ...

Obtain the longitude and latitude coordinates by calculating the distance and angle

I need to generate geographical combinations within a grid that covers my city. The grid measures 500m x 500m. I want to input starting longitude/latitude coordinates, distance (500m), degree (90 or 270), and receive the next longitude/latitude combination ...

Triggering events in server-side scripts involves activating specific actions to occur

Websites like SO have a feature where you receive notifications when your question gets answered, or when you earn a new badge. Additionally, if you receive a new private message in the forum, you are notified with an alert message. Whe ...

Getting information from a string object using regular expressions in Python

Here is a string that I need to work with: a = '91:99 OT (87:87)' My goal is to split it into separate numbers: ['91', '99', '87', '87'] Since the numerical values can range from 01 to 999, I will be usin ...

Once the form has been loaded without any information, go ahead and submit

I have created my index.php file with the following code: <script src="js/jquery-1.9.1.js"></script> <script> $(document).ready(function(){ $("#sload").load('save1.php',function(forma){ $(".csave").click(function(){ ...

Obtain the results of your query neatly organized in a visually appealing table

I need help with displaying query results in a table. The HTML form includes a dropdown menu for selecting an institution name, and the table should show the persons associated with that institution. After clicking the submit button, the query result from ...

Is it possible to access protected properties using the __get() method and the -> operator?

Our team has implemented lazyloading techniques to load sub-objects from the database. We are using the magic __get() method along with a database call. All properties are protected, so the __get method is called from outside the object. However, we have e ...

Obtain identical encryption results with CryptoJS (JavaScript) and OpenSSL (PHP)

I am in the process of integrating a PHP encryption function into a ReactJS application. I have a requirement to transmit the token in a specific format that was generated using the OpenSSL library function (openssl_encrypt). It has been observed that the ...

What is the best way to create user profiles using the URL domain.com/username rather than domain.com/profile.php?user=username?

Recently, I developed a website that features profiles for each member. The user data is stored in a MySQL database while their profile pictures are saved in a folder named "pictures" with the username as the filename. Each member's username is unique ...

Python throws an exception when attempting to access a dictionary key that does not exist

Currently working on a school project involving the development of a library management system. One of the main functionalities includes verifying if the book ID entered by a user exists in another dictionary (bkd) where book details are stored, with keys ...

Return data for Direct Payment with PayPal Payments Advanced: Understanding the process

I currently have PayPal Payments Advanced set up with hosted checkout pages. On the initial page of the paylinkpro.paypal.com site, users are presented with two options: Everything functions properly when a user chooses "Checkout w/ PayPal." However, I am ...

Executing a class method in the order of arguments as specified in the constructor

My approach involves creating a class where each keyword-only argument in the constructor corresponds to a specific class method. I have also implemented another method that runs these class methods sequentially. However, I am facing an issue with executin ...

Error 404 - Page Not Found

I've recently created a new controller in my project, but I'm encountering a Not Found (#404) error when I attempt to access it. The URL I am using is http://localhost/basic/web/index.php?r=users/index Below is the code for the controller: < ...

Unable to view images on Wordpress theme

I am currently facing an issue where some images in my asset folder are not displaying properly when I convert my HTML/CSS/JS template to Wordpress. The main problem is with the image that should show up when you first visit the website. Below is the CSS c ...