Python 从其他文件执行脚本

Python 从其他文件执行脚本,python,Python,我想知道是否可以将脚本导入/包含到另一个脚本中 在我的例子中,我的主脚本只要求用户做出选择。根据这个选择,我想包括另一个脚本 print (30 * '-') print (" M A I N - M E N U") print (30 * '-') print 'Information' print (30 * '-') print ("1. Choice 1") print ("2. Choice 2") print ("3. Choice 3") print (30 * '-') i

我想知道是否可以将脚本导入/包含到另一个脚本中

在我的例子中,我的主脚本只要求用户做出选择。根据这个选择,我想包括另一个脚本

print (30 * '-')
print ("   M A I N - M E N U")
print (30 * '-')
print 'Information'
print (30 * '-')
print ("1. Choice 1")
print ("2. Choice 2")
print ("3. Choice 3")
print (30 * '-')

is_valid=0

while not is_valid :
        try :
                choice = int ( raw_input('Enter your choice [1-3] : ') )
                is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
        except ValueError, e :
                print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
if choice == 1:
        # Here I want to include the other script from other file.

正如DyZ所说,看看导入模块

作为一个简单的示例,假设您具有以下文件夹结构

  • src/
    • main.py
    • 模/
      • __初始值
      • menu.py
如果在src下运行上述代码,则可以在
main.py
中导入
menu.py
,如下所示:

从moduleA导入菜单


注:参数可以很容易地解析为:

您必须学习编写和导入模块。这是另一个python脚本吗?如果是这样的话,看看上面@DyZ是怎么说的。如果是其他语言,如果没有更好的解决方案,shell调用可以工作。为什么缩进这么宽?好吧,谢谢我找到了。我刚刚编写了另一个文件的导入名,但是当我测试脚本时,它会直接执行另一个文件。或者我只想在用户选择它的情况下做。除了其他人所说的之外,它可能值得一看。谢谢,不,我没有这个结构。好吧,现在,我知道怎么做了。@a.lejeune626很好!由于我正在努力提高我的排名,如果这个解决方案对你有效,你能帮我标记一下吗?