1

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 ...

johannchopin
  • 13,720
  • 10
  • 55
  • 101
  • If you are using environment then try sending command to enable that first – Eternal Nov 03 '19 at 09:27
  • I'm sorry but I don't really understand what you mean ? – johannchopin Nov 03 '19 at 11:43
  • 1
    Try running your script using full path, like `shell_exec("/usr/bin/python3.5 test.py")` (put in the path that you need). This way you will make sure that you run whatever version of Python you need and will not be dependent on your shell settings. – igrinis Nov 03 '19 at 11:56
  • @igrinis Thank you it works. I should have tried that first. Post your answer so I can give to you my bounty ;) – johannchopin Nov 03 '19 at 12:49

3 Answers3

3

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!

igrinis
  • 12,398
  • 20
  • 45
2

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
Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
  • that means the user does not exist in the system. Like I said, the apache user can have different names depending on your distro, you will have to find out what is yours. – Lucas Meine Oct 30 '19 at 14:30
  • try this: https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as – Lucas Meine Oct 30 '19 at 14:31
  • I tried it but my apache user don't have password but he ask me one so I'm confused. Is there a way to just give acces to my python modules to apache user ? – johannchopin Oct 30 '19 at 14:35
  • 1
    Are you running the above command as root? It's required for it to work. If you run it as root, it shouldn't ask you for a password because it overrides /etc/passwd and start a bash shell as Apache – Lucas Meine Oct 30 '19 at 14:37
  • You can also find where your modules are installed: https://stackoverflow.com/questions/2927993/where-are-the-python-modules-stored and give apache read, and maybe write, permissions to that directory. – Lucas Meine Oct 30 '19 at 14:41
  • My apache user did not have the version of python with its modules in it that I was using. So I write an alias for it and when I tried to list all python modules with `help("modules")` I can see the module :/ So apache user has access but still not work – johannchopin Oct 30 '19 at 14:50
  • Check if apache user is using the correct version of python, that may be the problem. – Lucas Meine Oct 30 '19 at 14:56
  • He use the good one and has the module :( How is it possible ? – johannchopin Oct 30 '19 at 15:00
-1

**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.

Nkenyor
  • 29
  • 6