【发布时间】:2019-08-06 23:49:36
【问题描述】:
client.py
# Importing modules
import os
import asyncio
import sys
import Events
import glob
import Plugins
global FPath
global PlugPath
global CMDPath
FPath = os.path.dirname(os.path.abspath(__file__))
PlugPath = os.path.dirname(os.path.abspath(__file__)) + "\\Plugins"
Events.Startup()
global cmds
cmds = input("\\>")
global commandlst
commandlst = cmds.split()
command = commandlst[0]
runpy = "python " + str(FPath) + "\\" + str(command) + ".py"
if(os.path.isfile(FPath + "\\" + command + ".py") == True):
os.system(runpy)
else:
print(command + " is not a command or a plugin command!")
os.system("ping localhost -n 2 >nul")
os.system("py client.py")
help.py
import json
from pprint import pprint
import os
import sys
import client
commands = json.loads(open(sys.path[0] + "\\commands.json").read())
当它导入客户端时,它会运行整个 client.py 文件。 我已经尝试从客户端导入 commandlst 并且没有任何效果。 请帮忙。
【问题讨论】:
-
这就是 Python 的工作原理。当您在 Python 中导入模块时,它会执行该代码中的所有内容。如果您不想执行某些部分代码,则应将其写入
if __name__ == '__main__':范围内。
标签: python python-import