python - ModuleNotFoundError:没有名为“死”的模块
问题描述
我是 Python 的绝对初学者,我正在学习 Python 代码一个星期左右。我正在阅读 Python 速成课程 2nd。我在第 15 章遇到了一个滚动的 Python 脚本。脚本如下,在我运行它之后,它一直在说ModuleNotFoundError: No module named 'die'
。我确实尝试了书中发布的确切代码,但错误仍然存在。
import plotly as py
from random import randint
class Die:
"""A class representing a single die."""
def __init__(self, num_sides=6):
"""Assume a six-side die."""
self.num.sides = num_sides
def roll(self):
"""Return a random value between 1 and number of sides."""
return randint(1, self.num_sides)
from die import Die
# Create a D6
die = Die()
# Make some rolls, and store results in a list
results =[]
for roll_num in range(100):
result = die.roll()
results.append(result)
print(results)
解决方案
线
从模具进口模具
表示您Die
要从模块导入对象die
。
模块die
可能是一个文件die.py
。
在这种情况下,您希望Die
在一个名为的文件中定义您的类并从另一个文件中die.py
导入它 ( )。from die import Die
如果同一目录中没有文件die.py
并且没有调用模块die
,则会收到错误 ModuleNotFoundError。
推荐阅读
- c - 调用后出现段错误,但是一个非常相似的函数没有得到段错误错误
- r - ggplot:为什么x轴上的顺序不是水平而不是按字母顺序打印?
- r - 避免在ggplot2气球图中按字母顺序对X轴和y轴进行排序
- blockchain - 如何确保通知的比特币区块得到确认?
- python - 从列表中提取值并将其连接到字符串
- python - 如何在 Python 中对列表应用正则表达式时对其进行迭代?
- angular - 角度10中对象数组的升序或降序?
- apache-spark - 写入镶木地板文件时出现 dotnet-spark 异常
- vimeo - Vimeo /me/projects 分页
- azure-machine-learning-studio - MS Azure 自动 ML - 输出 JSON 作为文本发送