首页 > 解决方案 > 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)

标签: pythonpython-3.x

解决方案


线

从模具进口模具

表示您Die要从模块导入对象die

模块die可能是一个文件die.py

在这种情况下,您希望Die在一个名为的文件中定义您的类并从另一个文件中die.py导入它 ( )。from die import Die

如果同一目录中没有文件die.py并且没有调用模块die,则会收到错误 ModuleNotFoundError。


推荐阅读