我的使用的vsCode中无法导入我自定义的模块head.py,如图
虽然不能使用form [模块名] import*但是可以使用import
如图:
另外,我的Python版本为Python3.11.0
head.py代码:
## head.py
## Pyhton 3 语法代码文件
## 版权所有 Copyright ©
## Copyright © 2022 高宇涵.330205201010157010 All Rights Reserved.
import sys
import os
import time
import math
import cmath
import ctypes
import copy
import cmd
import string
import abc
import random
import collections
from collections import Counter,OrderedDict as Ordir
import json
import csv
import datetime
import threading
import enum
from enum import Enum,IntEnum
import const
try:
import urllib2
except ImportError:
import urllib
try:
import pygame
except ImportError:
pass
try:
import pygal
except ImportError:
pass
try:
import matplotlib
except ImportError:
pass
try:
import requests
except ImportError:
pass
def printf(*strings, sep:str='',end:str=''):
"""
默认末尾没有换行符的输出
"""
sept = sep
cstr:str = ''
for str in strings:
cstr += str
cstr += end
print(cstr,sep=sept, end='')
return 0
def fac(number:int):
"""
返回斐波那契(Fibonacci)数列中的第{number}员
"""
if number == 1:
return 1
if number == 2:
return 1
if number <= 0:
return None
else:
# number -= 1
return fac(number-2) + fac(number-1)
def Type_int(num:any):
"""int()的升级版"""
try:
return int(num)
except ValueError:
return int(float(num))
def Type_float(n:any):
"""给float()取别名"""
return float(n)
def Type_bool(n:any):
"""给bool()取别名"""
return bool(n)
def Input(str:str):
"""
兼容Python2与Python3的input()版本
"""
l:str
try:
l = raw_input(str)
except NameError:
l = input(str)
return l
def colorRGB(color_text=''):
"""
返回RGB颜色
"""
if color_text.lower() == 'white':
return (0,0,0)
if color_text.lower() == 'black':
return (255,255,255)
if color_text.lower() == 'yellow':
return (255,255,0)
if color_text.lower() == 'blue':
return (0,0,255)
if color_text.lower() == 'green':
return (0,255,0)
if color_text.lower() == 'orange':
return (255,128,0)
if color_text.lower() == 'purple':
return (255,0,255)
if color_text.lower() == 'orange red':
return (255,69,0)
if color_text.lower() == 'orange yellow':
return (254,183,42)
if color_text.lower() == 'yellow green':
return (128,255,0)
if color_text.lower() == 'green blue':
return (17,100,180)
if color_text.lower() == 'blue purple':
return (50,74,178)
if color_text.lower() == 'red purple':
return (9,164,174)
else:
return (None,None,None)
const.py源代码:
import sys
from threading import RLock
single_lock = RLock()
def Singleton(cls):
instance = {}
def _singleton_wrapper(*args, **kargs):
with single_lock:
if cls not in instance:
instance[cls] = cls(*args, **kargs)
return instance[cls]
return _singleton_wrapper
@Singleton
class Const:
# 自定义异常处理
class ConstValueError(PermissionError):
pass
class ConstCaseError(PermissionError):
pass
def __setattr__(self, name, value):
if name in self.__dict__:
raise self.ConstValueError("不能修改常量 {0} 的值 ".format(name))
if not name.isupper():
raise self.ConstCaseError("常量名称 {0} 必须大写".format(name))
self.__dict__[name] = value
sys.modules[__name__] = Const()
各位广大网友,请求回答,谢谢!