[英]Py file import stops importing
当我尝试在几次尝试后一起运行这两个脚本时,它停止工作了吗? 这是因为导入模块吗?
测试1.py
test = input("Go to new script?: ")
if test=="yes":
print("going to new script")
import test2
测试2.py
test = input("Go to old script?: ")
if test=="yes":
print("going to new script")
import test1
错误在于它结束了自己。
C:\Users\bj\Desktop>python test1.py
Go to new script?: yes
going to new script
Go to old script?: yes
going to new script
Go to new script?: yes
going to new script
C:\Users\bj\Desktop>
import
记住已经导入的文件,它不会再次导入它们。
最好将代码放在函数中,并将函数从第二个文件导入到第一个文件并循环运行。 第二个函数应该使用return
返回到第一个函数。
测试2.py
def func2():
while True:
answer = input("Go to old script?: ")
if answer.lower() == "y":
print("Going back to old script")
return
测试1.py
from test2 import func2
def func1():
while True:
answer = input("Go to new script?: ")
if answer.lower() == "y":
print("Going to new script")
func2()
func1()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.