👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m trying to run a Python script from PHP using the following command:
exec("/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2");
However, PHP simply doesn"t produce any output. Error reporting is set to E_ALL and display_errors is on.
Here"s what I"ve tried:
- I used
python2
,/usr/bin/python2
andpython2.7
instead of/usr/bin/python2.7
- I also used a relative path instead of an absolute path which didn"t change anything either.
- I tried using the commands
exec
,shell_exec
,system
.
However, if I run
if (exec("echo TEST") == "TEST")
{
echo "exec works!";
}
it works perfectly fine while shutdown now
doesn"t do anything.
PHP has the permissions to access and execute the file.
EDIT: Thanks to Alejandro, I was able to fix the problem. If you have the same problem, don"t forget that your webserver probably/hopefully doesn"t run as root. Try logging in as your webserver"s user or a user with similar permissions and try to run the commands yourself.
👻 Read also: what is the best laptop for engineering students?
Running a Python script from PHP absolute: Questions
How to get an absolute file path in Python
3 answers
Given a path such as "mydir/myfile.txt"
, how do I find the file"s absolute path relative to the current working directory in Python? E.g. on Windows, I might end up with:
"C:/example/cwd/mydir/myfile.txt"
Answer #1
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
"C:/example/cwd/mydir/myfile.txt"
Also works if it is already an absolute path:
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
"C:/example/cwd/mydir/myfile.txt"
Running a Python script from PHP absolute: Questions
How to check if a path is absolute path or relative path in a cross-platform way with Python?
3 answers
UNIX absolute path starts with "/", whereas Windows starts with alphabet "C:" or "". Does python have a standard function to check if a path is absolute or relative?
Answer #1
os.path.isabs
returns True
if the path is absolute, False
if not. The documentation says it works in windows (I can confirm it works in Linux personally).
os.path.isabs(my_path)
Running a Python script from PHP absolute: Questions
How to join absolute and relative urls?
3 answers
I have two urls:
url1 = "http://127.0.0.1/test1/test2/test3/test5.xml"
url2 = "../../test4/test6.xml"
How can I get an absolute url for url2?
Answer #1
You should use urlparse.urljoin :
>>> import urlparse
>>> urlparse.urljoin(url1, url2)
"http://127.0.0.1/test1/test4/test6.xml"
With Python 3 (where urlparse is renamed to urllib.parse) you could use it as follow:
>>> import urllib.parse
>>> urllib.parse.urljoin(url1, url2)
"http://127.0.0.1/test1/test4/test6.xml"
We hope this article has helped you to resolve the problem. Apart from Running a Python script from PHP, check other absolute-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Running a Python script from PHP
- Deutsch Running a Python script from PHP
- Français Running a Python script from PHP
- Español Running a Python script from PHP
- Türk Running a Python script from PHP
- Русский Running a Python script from PHP
- Português Running a Python script from PHP
- Polski Running a Python script from PHP
- Nederlandse Running a Python script from PHP
- 中文 Running a Python script from PHP
- 한국어 Running a Python script from PHP
- 日本語 Running a Python script from PHP
- हिन्दी Running a Python script from PHP
Paris | 2023-03-26
Python functions is always a bit confusing 😭 Running a Python script from PHP is not the only problem I encountered. Will get back tomorrow with feedback
California | 2023-03-26
I was preparing for my coding interview, thanks for clarifying this - Running a Python script from PHP in Python is not the simplest one. I am just not quite sure it is the best method
Tallinn | 2023-03-26
Maybe there are another answers? What Running a Python script from PHP exactly means?. Checked yesterday, it works!