首页 > 解决方案 > 如何正确导入我创建的 python 模块?

问题描述

我的主 python 文件和我的模块 python 文件都在同一个目录中。当我尝试通过调用模块文件中的类来定义新变量时,VSCode 告诉我该变量未定义。

模块.py

class Shirt:

    def __init__(self, shirt_color, shirt_size, shirt_style, shirt_price):
        self.color = shirt_color
        self.size = shirt_size
        self.style = shirt_style
        self.price = shirt_price

    def change_price(self, new_price):

        self.price = new_price

    def discount(self, discount):

        return self.price * (1 - discount)

衬衫.py

import os
import shirt_module

new_shirt = Shirt('red', 'S', 'short-sleeved', 15)

VSCode 是说 shirt.py 文件中的变量 Shirt 是未定义的。有谁知道为什么会发生这种情况?

标签: python-3.x

解决方案


推荐阅读