如何链接多个python脚本 [英] how to link multiple python scripts
问题描述
我是python的新手,我想将我的函数分成不同的文件,比如我用c ++(一个驱动文件和一个用于不同类别的函数的文件,我最终在编译时将它们连接在一起)。
假设我想创建一个启动主程序的简单'驱动程序'文件和一个'函数'文件,其中包含由驱动程序和函数内的其他函数调用的简单函数'file。
我应该怎么做?由于python没有编译,所以我怎么把文件链接在一起?
c $ c>模块。简单地创建不同的python文件并在脚本开始时导入它们。例如,我得到了这个 function.py
file:
def func(a,b):
返回a + b
以下是 main.py
档案:
导入函数
if __name__ ==__main__:
ans = function.func(2,3)
print ans
就是这样!这是关于导入模块的官方教程。
i am new to python and i would like to seperate my functions into different files like i do with c++ (a driver file and a file for different categories of functions that i end up linking together upon compilation).
suppose i want to create a simple 'driver' file which launches the main program and a 'function' file which includes simple functions which are called by the driver and other functions within the 'function' file.
how should i do this? since python is not compiled, how do i link files together?
You can import
modules. Simply create different python files and import them at the start of your script.
For example I got this function.py
file :
def func(a, b):
return a+b
And this main.py
file:
import function
if __name__ == "__main__":
ans = function.func(2, 3)
print ans
And that is it! This is the official tutorial on importing modules.
这篇关于如何链接多个python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!