skip to Main Content

I’ve been searching around for awhile and found some similar threads as well as the recommended reviews but non seem to be like my issue as my php does call the python script but it doesn’t work when I try to "import" a library. Meaning, if I use a basic python script to write a text file, but PHP button calls the script and it works. If I change the script to be more like the intended purpose and us a "import" library, it stops working. But I can use the script at the command line. OSX.

I’ve got a simple localhost website with a php page that has some buttons on it that call python scripts. I can run it at the terminal and the output works so standalone the script is fine, it’s only when I add a simple "import sys" or "import whois" that it immediately breaks

I had it working on an older laptop with a previous PHP and Python version.

Here is the php

 <?
 php $command = escapeshellcmd("/usr/bin/python3 test-py.py");
 $output = shell_exec($command); echo $output;
 ?>

here is the simple python whois to a output file

import sys
import whois

if len(sys.argv) != 2:
    print("Usage: python whois_ip.py <ip_address>")
    sys.exit(1)

ip_address = sys.argv[1]
w = whois.whois(ip_address)

with open("output.txt", "w") as f:
f.write(str(w))

print("WHOIS lookup results written to output.txt")`

The above doesn’t work but if I just do a simple fwrite to output.txt it will work. It’s the Import statements that break it

Here is some additional supporting information

Run from command line as local user

() -> python3 -c "import site;print(site.getsitepackages())"
['/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages', '/Library/Python/3.9/site-packages', '/AppleInternal/Library/Python/3.9/site-packages', '/AppleInternal/Tests/Python/3.9/site-packages']

As Daemon from the PHP execshellcmd

() -> cat output.txt
['/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages', '/Library/Python/3.9/site-packages', '/AppleInternal/Library/Python/3.9/site-packages', '/AppleInternal/Tests/Python/3.9/site-packages']

My questions are

  1. Any thoughts why the import function no longer works? Could it be a permissions issue with importing libraries?
  2. My PHP.ini has disabled_functions = "" (so nothing is disabled right?)
  3. Is there something special about PHP 8.2.5 and Python 3.9.6 that I should be aware of?

Many thanks, especially for advice how to get it working..

Previously working PHP calling Python, not it no longer works.

2

Answers


  1. Chosen as BEST ANSWER

    I may have found my solution and sharing here.

    Inside my XAMPP config file, there is the httpd.conf where you set the user. Mine was set to daemon. I changed it to my local username and staff is the group. That allows the extra functions of my script to work.

    Sharing here for others that may have similar issues.

    In short, it was a permissions issue with the user that was running httpd


  2. To run or fetch data from a Python file through a shell command you must have to write the specific python.exe file location at the top of the file like below:

    #!C:/Program Files/Python311/python

    #!            C:/Program Files/Python311/         python
    (default)       (python.exe location)            (default)
    

    Then must have to add print() after initialized python.exe file location.As like:

    #!C:/Program Files/Python311/python
    print()
    

    Later only write:php $command = escapeshellcmd("python test-py.py");

    instead of php $command = escapeshellcmd("/usr/bin/python3 test-py.py");

    **Then if you want to import module, you must have installed that module.
    Like the sample code:

    #!C:/Program Files/Python311/python 
    print()
    import sys
    import whois
    

    Below we added the process of installing whois module**

    Install pip on Windows:

    1. Download the get-pip.py file from https://bootstrap.pypa.io/get-pip.py.
    2. Open a command prompt as an administrator.
    3. Navigate to the directory where the get-pip.py file is saved using the ‘cd’ command.
    4. Run the following command to install pip: python get-pip.py
    5. Verify that pip is installed correctly by running the command pip --version

    Install pip on macOS:

    1. Open a terminal window.
    2. Install Xcode Command Line Tools by running the command xcode-select --install
    3. Install Homebrew by running the command /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
    4. Install pip using Homebrew by running the command brew install python
    5. Verify that pip is installed correctly by running the command pip --version
      Note: On macOS, you can also install pip using the built-in Python installation by running the command sudo easy_install pip, but it’s recommended to use Homebrew instead.

    Install whois on Windows:

    To install whois on Windows and macOS, you can use pip, which is the Python package manager. Here are the steps to install whois using pip:
    On Windows:

    1. Open Command Prompt or PowerShell as an administrator.
    2. Type pip install python-whois and press Enter.
    3. Wait for the installation to complete.
    4. To check if whois is installed, type python and press Enter to open the Python interpreter.
    5. Type import whois and press Enter. If there are no error messages, whois has been successfully installed.

    Install whois on macOS:

    1. Open Terminal.
    2. Type pip install python-whois and press Enter.
    3. Wait for the installation to complete.
    4. To check if whois is installed, type python and press Enter to open the Python interpreter.
    5. Type import whois and press Enter. If there are no error messages, whois has been successfully installed.
      Note: If you have multiple versions of Python installed on your system, you may need to use pip3 instead of pip to install whois for Python 3.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search