唯一导入 * 仅允许在模块级别
Unique import * only allowed at module level
我正在制作一个实用程序,其中内置了多个程序,但我对我的程序进行了一些更改,重新 运行 当用户提示时,出于某种原因,我面临着错误
import * only allowed at module level
这是我的代码
def main():
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
print("*"*30)
print("Utility Build v1: Starting")
print("*"*30)
time.sleep(3)
print("NOTE: Before using this program for repairing corrupted disk, please locate this utility .py file into the corrupted storage. Thanks")
time.sleep(3)
print("*"*30)
print("*"*30)
print("Commands: Choose by inputting allocated number")
print("Utility 1: Speed Test")
print("Utility 2: Whatsapp Message Automation")
time.sleep(2)
print("Please Wait...Loading")
time.sleep(4)
print("Utility 3: Disk/Removable Storage Repair(a.k.a Dr Corrupt)")
print("Utility 4: Python .py status monitor")
print("*"*30)
print("*"*30)
print("q = Quit Utility Program")
input_ = input(": ")
if input_ == "q":
exit()
if input_ == "1":
time.sleep(2)
print("*"*30)
print("Speed Test: Starting")
print("*"*30)
st = Speedtest()
Download_ = print("Your connection download speed is:", st.download())
Upload_ = print("Your connection upload speed is:", st.upload())
Download1_ = st.download()
Upload1_ = st.upload()
print("*"*30)
print("Speed Test: Finishing Up!")
print("*"*30)
answer = input("Would you like results? ")
if answer == "yes":
print("NOTE: The first 2 digits frm the left is your internet speed")
time.sleep(2)
top = Tk()
top.geometry("100x100")
messagebox.showinfo("Speed Test: Download", Download1_)
top.mainloop()
reply = input("Would like to leave Utility Build(yes) or go to home page?(no) ")
else:
reply1 = print("Would like to leave Utility Build(yes) or go to home page?(no) ")
if reply1 == "yes":
main()
else:
exit()
if input_ == "2":
whatsapp.sendwhatmsg("+61450776320", "Hi, this is a test", 0, 0)
if input_ == "3":
for filename in listdir('./'):
if filename.endswith('.png'):
try:
img = Image.open('./'+filename) # open the image file
img.verify() # verify that it is, in fact an image
except (IOError, SyntaxError) as e:
print('Bad file:', filename) # print out the names of corrupt files
“模块级别”仅表示不在 class 或函数中的脚本部分。您在那里定义的任何名称都直接进入模块命名空间。
因此错误消息只是说移动
def main():
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
至
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
def main():
实际上,解释器真正关心的只是行from tkinter import *
。其他的是约定俗成和可读性的问题。
CPython 对函数内部的局部名称空间进行优化,要求解释器知道所有局部变量的名称 up-front。星号导入可以防止这种情况发生,因为在 运行 之前您不知道导入的模块中会有什么名称。全局命名空间没有这个限制,所以你可以在那里做星号导入。
Python 是成人同意的语言。仅仅因为某些东西是“不好的做法”或不可维护的并不会使它成为语法错误。
我正在制作一个实用程序,其中内置了多个程序,但我对我的程序进行了一些更改,重新 运行 当用户提示时,出于某种原因,我面临着错误
import * only allowed at module level
这是我的代码
def main():
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
print("*"*30)
print("Utility Build v1: Starting")
print("*"*30)
time.sleep(3)
print("NOTE: Before using this program for repairing corrupted disk, please locate this utility .py file into the corrupted storage. Thanks")
time.sleep(3)
print("*"*30)
print("*"*30)
print("Commands: Choose by inputting allocated number")
print("Utility 1: Speed Test")
print("Utility 2: Whatsapp Message Automation")
time.sleep(2)
print("Please Wait...Loading")
time.sleep(4)
print("Utility 3: Disk/Removable Storage Repair(a.k.a Dr Corrupt)")
print("Utility 4: Python .py status monitor")
print("*"*30)
print("*"*30)
print("q = Quit Utility Program")
input_ = input(": ")
if input_ == "q":
exit()
if input_ == "1":
time.sleep(2)
print("*"*30)
print("Speed Test: Starting")
print("*"*30)
st = Speedtest()
Download_ = print("Your connection download speed is:", st.download())
Upload_ = print("Your connection upload speed is:", st.upload())
Download1_ = st.download()
Upload1_ = st.upload()
print("*"*30)
print("Speed Test: Finishing Up!")
print("*"*30)
answer = input("Would you like results? ")
if answer == "yes":
print("NOTE: The first 2 digits frm the left is your internet speed")
time.sleep(2)
top = Tk()
top.geometry("100x100")
messagebox.showinfo("Speed Test: Download", Download1_)
top.mainloop()
reply = input("Would like to leave Utility Build(yes) or go to home page?(no) ")
else:
reply1 = print("Would like to leave Utility Build(yes) or go to home page?(no) ")
if reply1 == "yes":
main()
else:
exit()
if input_ == "2":
whatsapp.sendwhatmsg("+61450776320", "Hi, this is a test", 0, 0)
if input_ == "3":
for filename in listdir('./'):
if filename.endswith('.png'):
try:
img = Image.open('./'+filename) # open the image file
img.verify() # verify that it is, in fact an image
except (IOError, SyntaxError) as e:
print('Bad file:', filename) # print out the names of corrupt files
“模块级别”仅表示不在 class 或函数中的脚本部分。您在那里定义的任何名称都直接进入模块命名空间。
因此错误消息只是说移动
def main():
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
至
import os
import sys
import time
import pywhatkit as whatsapp
from speedtest import Speedtest
from tkinter import *
from tkinter import messagebox
from os import listdir
from PIL import Image
def main():
实际上,解释器真正关心的只是行from tkinter import *
。其他的是约定俗成和可读性的问题。
CPython 对函数内部的局部名称空间进行优化,要求解释器知道所有局部变量的名称 up-front。星号导入可以防止这种情况发生,因为在 运行 之前您不知道导入的模块中会有什么名称。全局命名空间没有这个限制,所以你可以在那里做星号导入。
Python 是成人同意的语言。仅仅因为某些东西是“不好的做法”或不可维护的并不会使它成为语法错误。