As you can see from the title, it is a request that is too niche, but I got caught in various errors on the way, so I have summarized it for myself in the middle.
You can use the exec command to execute commands in PHP as you would on the command line, not just python functions. (See https://www.php.net/manual/ja/function.exec.php)
exec($command, $output, $result_code);
Pass the command you want to execute to \ $ command. \ $ output and \ $ result_code are optional, but \ $ output stores the command execution result as an array, and \ $ result_code stores the code indicating whether the command execution was successful.
For example
exec('ls', $output, $result_code);
When you execute, the command "ls" that displays the files and directories in the current directory is executed, and the result is stored in \ $ output. When successful, \ $ result_code seems to be 0. (* Confirmation required)
Consider the following python file hello.py.
hello.py
def hello(name):
print(name)
Just take the arguments and print.
To do this from the command line:
# python -c 'import hello; hello.hello("Alice")'
If you give this command as \ $ command and the above-mentioned exec command argument, you can execute the python function from PHP. However, to execute the function this way, you need to run the command in the same directory as hello.py.
I'm not familiar with python, so here's a random and successful method.
-Using $ PYTHONPATH
Recognition of PATH to refer to when executing python.
# export PYTHONPATH=<The path you want to specify>:${PATH}
By doing so, you will be able to refer to the path you want to specify. This time it's a good idea to specify the directory that contains hello.py. That way, you can run the hello function with the python -c command from another directory that doesn't contain hello.py.
I think that you often operate a web server when you are using PHP. There is a difference between simply executing the same command from the command line on the server as described above and executing the same command from the web server. (See https://sierra-kilo.hatenablog.jp/entry/2017/08/11/233708)
It seems that the path specified in $ PYTHONPATH is not inherited when executing from the Web server. In the reference article, I am dealing with it by rewriting the apache configuration file, but this time I will change the setting in/etc/sysconfig/httpd. (See http://yukke.blog3.fc2.com/blog-entry-68.html)
In this reference article
export ORACLE_HOME=/oracle/home/
By doing so, the environment variable ORACLE_HOME is added, but the environment variable I want to add this time is PYTHONPATH. Also, for some reason export didn't work, so I did the following.
PYTHONPATH=<The path you want to specify>:${PATH}
After rewriting up to this point, restart apache and check. Php.info () is useful for checking environment variables. (Details of how to use are omitted)
If PYTHONPATH is added to the environment variable, you will be able to refer to the specified path from the web server, so you will be able to execute python functions.
This time, there were some points that got stuck for one purpose, so I summarized them all at once, but I think that they could be used independently again.
To summarize briefly
--How to use the exec function --How to use the python -c command --Specify PYTHONPATH and execute from another directory --Add environment variables when running from a web server
was.
I study every day.
Recommended Posts