当我导入模块时,它会运行整个python文件

当我导入模块时,它会运行整个python文件,python,python-import,Python,Python Import,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(__fil

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")
救命啊

导入json
从pprint导入pprint
导入操作系统
导入系统
导入客户端
commands=json.load(打开(sys.path[0]+“\\commands.json”).read())
导入客户端时,它将运行整个client.py文件。 我已经尝试从客户端导入commandlst,但没有任何效果。
请帮助。

这就是导入的工作原理。要解决您的特定问题,请不要运行整个
client.py
模块,您可以将其内容放入
中,如果
name\u=='\u\u main\u':检查如下:

# Importing modules
...
if __name__ == '__main__':
    global FPath
    ...

Python就是这样工作的。在Python中导入模块时,它会执行该代码中的所有内容。如果不希望执行部分代码,则应将其写入
范围内,如果uuuu name_uuuuu=='\uuuuu main_uuuu':
scope。