简体   繁体   English

如何将参数从服务器上的一个python脚本传递给另一个?

[英]How to pass arguments from one python script on the server to another?

I have 2 Python scripts on my server. 我的服务器上有2个Python脚本。 I want to run the second one, SendMail.py , from MainScript.py , and I need to pass 2 arguments to it. 我想从MainScript.py运行第二个SendMail.py ,我需要传递2个参数。

I want to call send_mail function in MainScript.py : 我想在MainScript.py调用send_mail函数:

def send_mail(subject, body):
    # call SendMail.py with and pass the subject & body arguments

so it will run SendMail.py . 所以它将运行SendMail.py

How can I run the other script while passing the arguments? 如何在传递参数时运行其他脚本? Is it possible to do without importing the main function from SendMail.py into MainScript.py ? 是否可以不将SendMail.py的main函数导入MainScript.py How do I grab the arguments in SendMail.py ? 如何获取SendMail.py的参数?

You can use the subprocess module to run another python interpreter; 您可以使用subprocess模块运行另一个python解释器; sys.executable gives you a handy starting point: sys.executable为您提供了一个方便的起点:

import subprocess
import sys

subprocess.call([sys.executable, 'SendMail.py', subject, body])

but best practice would be to just import SendMail ; 但最佳做法是只导入SendMail ; make sure you structure it to only run 'script-like' code with a if __name__ == '__main__' guard, then import the main 'sending' function from SendMail and re-use that: 确保你将它构造成仅运行带有if __name__ == '__main__'保护的if __name__ == '__main__'类似脚本”的代码,然后从SendMail导入主“发送”功能并重新使用:

def send_mail(subject, body):
    # main sending function


if __name__ == '__main__':
    # parse command line arguments into subject and body
    send_mail(subject, body)

then import SendMail and call SendMail.send_mail(subject, body) . 然后导入SendMail并调用SendMail.send_mail(subject, body)

Use subprocess module: 使用子进程模块:

import subprocess
args = [subject, body]
subprocess.call(['python','SendMail.py'] + args)

Inside SendMail.py use sys.argv : SendMail.py里面使用sys.argv

import sys
subject, body = sys.argv[1:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将命令行参数从一个python模块传递到另一个python模块 - How to pass command line arguments from one python module to another 如何在Python中将用户输入从一个脚本传递到另一个脚本? - How to pass user input from one script to another in Python? 如何将变量从一个Python脚本传递到另一个脚本? - How to pass a variable from one Python script to another? 如何在Python中将很长的字符串从一个脚本传递到另一个脚本? - How to pass a really long string from one script to another in Python? 如何将变量从一个 Python 脚本传递到另一个? - How can I pass a variable from one Python Script to another? 如何从另一个脚本中将命令行 arguments 传递给 python 脚本? - How to pass command-line arguments to a python script from within another script? 如何在python脚本中将随机参数传递给另一个python程序? - how to pass random arguments to another python program in python script? 如何调用另一个Python 3脚本并将其传递一些参数 - How to call another Python 3 script and pass it some arguments python:从一个函数传递多个参数到另一个函数 - python: pass multiple arguments from one function to another 如何将变量参数从bash脚本传递给python脚本 - How to pass variable arguments from bash script to python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM