[英]How to run script from environment in Python?
I have got recommendations to run Python script on Linux Server using this sequence:我得到了使用以下序列在 Linux 服务器上运行 Python 脚本的建议:
1) source path to environment 1)环境的源路径
2) python path to python script 2) python脚本的python路径
So, I need to call this script from php
command system
:所以,我需要从
php
命令system
调用这个脚本:
Should it be like this?应该是这样吗?
system("source *path to environment* python *path to python script*")
How to set environment and then run script?如何设置环境然后运行脚本?
system("*path/to/environment*/bin/python *path to python script*")
is enough.足够。 The trick is to run the script using python from the environment;
诀窍是从环境中使用 python 运行脚本; that way you don't need to
source
the activation script.这样您就不需要获取激活脚本的
source
。
Your python script need to contain 'shebang' string at the top #!/usr/bin/env python你的python脚本需要在顶部包含'shebang'字符串#!/usr/bin/env python
for example file start.py :例如文件start.py :
#!/usr/bin/env python
def some_func():
print("Run some python function")
if __name__ == "__main__":
some_func()
then also you need to make it executable with command:然后您还需要使用命令使其可执行:
chmod +x start.py
and you can run it:你可以运行它:
./start.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.