nanocode 2023-02-03 13:53 采纳率: 75%
浏览 21
已结题

无法导入head.py

我的使用的vsCode中无法导入我自定义的模块head.py,如图

img


虽然不能使用form [模块名] import*但是可以使用import
如图:

img


另外,我的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()

各位广大网友,请求回答,谢谢!

  • 写回答

2条回答 默认 最新

  • 元气少女缘结神 2023-02-03 14:28
    关注

    你写的form 不是from ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月11日
  • 已采纳回答 2月3日
  • 创建了问题 2月3日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度