0
votes

I have a Python script, which works fine when I run it in Terminal. Here it is:

import bs4, requests, json, sys
from sys import argv

def getFsmonData(link):
    res = requests.get(link)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text)

    # Get table data
    tds = soup.select('td')
    new_td_list = []
    for i in range(len(tds)):
        inner_td_list = []
        for y in range(len(tds)):
            inner_td_list.append(tds[y].getText())
        new_td_list.append(inner_td_list)
    print(inner_td_list)
    td_list_json = json.dumps(inner_td_list)
    tdf = open("/path/to/file/data/td_data.txt", "w")
    tdf.write(td_list_json)
    tdf.close()

getFsmonData(sys.argv[1])

But now I am trying to run it from PHP and I get the following error:

Traceback (most recent call last): File "/path/to/file/example.py", line 1, in import bs4, requests, json, sys ModuleNotFoundError: No module named 'bs4'

I guess PHP thinks I do not have this module installed, but I am not sure of course. Here is my PHP code:

<?php
    $link = $_POST['dn-link'];

    if (isset($_POST['submit'])) {
        system('/usr/bin/python3 /path/to/file/example.py ' . escapeshellarg($link) . ' 2>&1', $retval);
    }
?>

Who can help solving this issue?

2
Your script is running as a different user when called from PHP than when called from the command line, so you need to set up that user's Python environment so that it can find the module. - kindall
Why downgrading? People who downgrade this act as if this is a general knowledge question. I am asking because I cannot figure it out on my own probably. - tera_789
@kindall You want to say that if I run it as sudo, it will work? Well it does not - tera_789
@tera_789, it looks to me like you are trying to run a python file, instead of the python interpreter. I'm not one hundred percent sure by your PHP code. Are familiar you with the protocol for starting sub processes on your OS? In general you have to start the interpreter and pass the file to the interpreter, not run the .py file directly. Usually this requires you to have previously added the interpreter as an environment variable with the appropriate permissions. - Jamie Marshall
Write a simple script that just prints sys.path and run it both in your PHP environment and at your usual command line. - kindall

2 Answers

1
votes

Most likely, when your PHP script executes your Python script, its executing it as a different user (possibly super user). What you can do is check if the packages are installed under the super user by doing

sudo pip list

or if you're using python3

sudo pip3 list

see if the packages you we're wanting to use are listed from this command.

If they aren't listed, you can easily install them via the command:

sudo pip install <pkg name>

or

sudo pip3 install <pkg name>

What you're doing here is installing the packages under the super user rather than you the local user.

1
votes

I think your misunderstanding the permission issue. It's not that you need to add it PHP. It's that your OS keeps track of permissions, and the permission you have assigned to your PHP interpreter and your Python interpreter are misaligned. There are two permissions that usually people mix up here - USER which is login level access and SYSTEM which is system wide access. If your on your own dev machine then add both to SYSTEM.

We need to know your OS to be able to fully answer, i'm going to guess Mac OS in which case would be this (I think, can't test it right now).

sudo nano "pathOfYourInterpreter"

The other thing could be happening is that the libraries you're tryng to import are somewhere where the current sys path you have setup can't reach them. For example if you have your sys variable for Python set to the directory where the interpretor is but the scripts you're importing are in a different folder. In that case you have several options for accessing those scripts.

  1. Set the current working directory to the folder where they're located before importing with:

    os.chdir("locationOfScript/")
    import script.py
    
  2. Add a sys Path at runtime:

     sys.path.append("locationOfScript/)
    

There are more methods of attack, but I think this should get you started.