简体   繁体   English

如何从 Python 环境中运行脚本?

[英]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.

相关问题 如何在没有cmd的虚拟环境中使用python运行python脚本 - How to run python script with python from virtual environment without cmd 如何从 MATLAB 在 Conda 环境中运行 Python 脚本? - How to run a Python script in a Conda environment from MATLAB? 如何从具有其他环境的另一个脚本中运行具有其环境的python脚本? - How to run a python script with its environment from another script with another environment? 加载环境以运行Python脚本的成本是多少? - How expensive it is to load the environment to run a Python script? 在Python环境中运行Python脚本? - Run Python script in Python environment? 从Python提示符运行Python脚本,以便将变量加载到交互式环境中 - Run a Python script from Python prompt such that variables are loaded into the interactive environment 如何进入一个Python虚拟环境并从Shell脚本中运行Shell命令? - How does one enter a Python virtual environment and run shell commands in it from a shell script? 如何通过任务计划程序计划从虚拟环境运行的python脚本 - How to schedule a python script to run from virtual environment via task scheduler 在python脚本中,更改用户,设置环境并运行几个命令 - From a python script, change user, set environment and run a couple of commands 在与Jupyter Notebook不同的环境中运行python脚本 - Run a python script in a different environment from a Jupyter Notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM