python-3.x - 如何正确导入我创建的 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 是未定义的。有谁知道为什么会发生这种情况?
解决方案
推荐阅读
- javascript - 我应该尝试在 Typescript 中强制执行封装吗?
- java - 如何将随机数生成器中的数字相加
- javascript - 如何为视差添加平滑运动?
- java - 通过 native-image 编译 Spring Boot 应用程序
- python - 我应该如何解决错误_tkinter.TclError:无法打开“sample.png”:没有这样的文件或目录
- vb.net - devExpress searchLookupEdit 没有设置值
- github-enterprise - in the github enterprise UI, is there a way to see commit statuses outside of the PR context?
- java - 如何在 Java 字符串中编码 unicode 字符,如 \u0022 和 \u005c(\ 和“)?
- yaml - 在yaml中预先声明一个排除的锚标头?
- python - 计算networkx图中开放三角形的数量