简体   繁体   English

如何从不同的脚本在 python 中运行 3 个函数?

[英]How can I run 3 functions in python from a different script?

I have 3 functions in a python script that I would like to run at the same time but from another python script, for example:我在 python 脚本中有 3 个函数,我想同时运行它们,但来自另一个 python 脚本,例如:

def a():
       print("\nphrase1")
def b():
       print("\nphrase2")
def c():
       print("\nphrase3")

I would like to run those 3 functions from a different file.我想从不同的文件中运行这 3 个函数。 Could anyone support me on that?有人可以支持我吗?

I suggest you copy the program with the functions to the same folder as the program you want to run them我建议您将具有这些功能的程序复制到与您要运行它们的程序相同的文件夹中

from yourprogram import a, b, c
#code
a()
b()
c()

Suppose if all above function is inside module fun.py then use below code snippet to run all of it -假设如果 function 以上的所有内容都在模块fun.py ,那么使用下面的代码片段来运行所有这些 -

import fun
for i in dir(fun):
    item = getattr(fun,i) 
    if callable(item):
        item()

dir(fun) retrieves all the attributes of module fun. dir(fun) 检索模块 fun 的所有属性。 If the attribute is a callable object, call it.如果属性是可调用的 object,则调用它。 Just a note, it will call everything which is callable in the fun module .请注意,它将调用 fun 模块中可调用的所有内容

Hope this answers your question.希望这能回答你的问题。

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

相关问题 (多线程-Python)如何创建一个运行两个脚本的脚本,我通常从两个不同的终端运行这些脚本? - (Multithreading-Python) How I can create a script that run two scripts which I usually run from two different terminals? 如果我的脚本使用从其他文件导入的函数,如何让我的脚本从 Unix 上的可执行 python 文件运行? (ModuleNotFoundError) - How can I make my script run from executable python file on Unix if it uses functions imported from other files? (ModuleNotFoundError) 如何在Kodi中运行python脚本? - How can I run a python script from within Kodi? 如何从Ubuntu Dash运行Python脚本? - How can I run a Python script from Ubuntu Dash? 如何从python脚本运行交互式docker shell - How can I run interactive docker shell from python script 如何从 pycharm python 脚本运行“pip install”? - How can i run "pip install" from a pycharm python script? 如何通过 typescript 中的巧克力蛋糕运行 python 脚本? - How can I run a python script through brownie from a typescript? 如何从 Python 脚本中运行 Streamlit 应用程序? - How Can I Run a Streamlit App from within a Python Script? 如何从 Flask 中运行 python 脚本 - How can I run a python script from within Flask 如何在ubuntu上从python运行casperjs脚本? - How can i run casperjs script from python on ubuntu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM