skip to Main Content

I wrote a python script that import a specific module that I have installed with pip.

When I run this script on terminal like python test.py everything work fine and when I run help("modules") I can see my module in the list (so he’s well installed)

Problem is that I try to run this script with an php script using shell_exec("python test.py") but then I get the error that python don’t know this module:

ImportError: No module named …

Do you have any idea what my mistake is or how i can resolve this problem ?


> Edit 1 :

So I became apache user like @LucasMeine told me and first I see that the python version used was not the right one. So I created an alias to the good one and then I checked with the apache user and the right python version that the module exist using help("modules") in the terminal. The module was in the given list so I don’t understand why in my apache error logs I get the message:

ImportError: No module named …

3

Answers


  1. Thing is, when you use shell_exec, the apache user is the one who will run the process. And the apache user does not have access to the modules you just installed with your regular user using pip.

    So you can just become the apache user:

    su -s /bin/bash apache
    

    install your stuff with pip, and then your code should work. Also have in mind that apache user can have different names depending on your linux distro.

    If you need to find what is your apache user, check this out: https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as

    and if your apache user is different, use the same command as above, but with the correct user. For example, if your apache user is httpd:

    su -s /bin/bash httpd
    
    Login or Signup to reply.
  2. When calling shell_exec("python test.py") you are relying on the shell settings that will expand you python command into the fully qualified path. Obviously, that if you have discrepancies between the settings of you accounts, you might invoke different version of Python, with different modules installed. To circumvent this dependency, you can try to specify the exact version of Python you want to run by using full path, something like

    shell_exec("/usr/bin/python3.5 test.py") 
    

    To get the path to your Python installation you can run which python in your terminal.

    As another path of solution, you can fix the shell settings of the account that runs web server to match those of your current user.

    Cheers!

    Login or Signup to reply.
  3. **Package Location: **
    I think the main solution is installing the package in a path that does not require a lot of permissions to access.

    Install the package in a path that "shell_exec" without the unique user privilege can access. Let’s play around:

    whereis PACKAGE e.g

    whereis openai
    

    You should see something like this: /home/USER/.local/bin/openai

    If that’s the output on the terminal, the code should work well if you are signed-in as that user but running it through a PHP/APACHE service should not work.

    You need to make a copy of the package to /usr/local/bin/openai. So copy and paste the package file into the /usr/local/bin/ path.

    **Run the correct Version: **
    Also make sure you are calling the correct version of Python in your exec, shell_exec or system command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search