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
- Any thoughts why the import function no longer works? Could it be a permissions issue with importing libraries?
- My PHP.ini has disabled_functions = "" (so nothing is disabled right?)
- 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
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
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
Then must have to add
print()
after initializedpython.exe
file location.As like: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:
Below we added the process of installing whois module**
Install pip on Windows:
get-pip.py
file is saved using the ‘cd’ command.install pip: python get-pip.py
pip --version
Install pip on macOS:
xcode-select --install
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install python
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:
pip install python-whois
and press Enter.Install whois on macOS:
pip install python-whois
and press Enter.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.