importTerror:在Python文件中导入类时没有名为“”的模块

2024-10-04 01:33:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程新手。我做了一个叫厨房的包。我想通过__init__.py文件导入一个类文件。

我是python版本:3.3.2

操作系统平台:windows

冰箱.py

class Fridge:   
    def __init__(self, items={}):
        """Optionally pass in an initial dictionary of items"""
        if type(items) != type({}):
            raise TypeError("Fridge requires a dictionary but was given %s" %
    type(items))
        self.items = items
        return

    def _Get_Items(self):
        print(self.items);

    def _Get_Added_Values(self,lst):
        values =0;
        print(len(lst));
        for index in lst:
            values += index;
        return values
    def _Get_Seperetor(self,str1,lst):
        str1=str1.join(lst);
        return str1;


    def _Get_Keys(self):
        print(self.items.keys());

课程.py文件

class Courses:
    def __init__(self, items=[]):
        """Optionally pass in an initial dictionary of items"""
        if type(items) != type([]):
            raise TypeError("Fridge requires a dictionary but was given %s" %
    type(items))
        self.items = items
        return

    def _Get_Items(self):
        print(self.items);

    def _Get_Seperetor(self,str1,lst):
        str1=str1.join(lst);
        return str1;


    def _Get_Keys(self):
        print(self.items.keys());

__init__.py

from Courses import Courses
from Fridge import Fridge

这些是存放在厨房的文件是

import Kitchen

执行此命令时,出现以下错误

Traceback (most recent call last): File "", line 1, in import Kitchen File "E:\Mani\Learnings\Phython\Kitchen__init__.py", line 1, in from Courses import Courses ImportError: No module named 'Courses'

请帮助我如何处理这件事,也请告诉我哪里出错了


Tags: inpyimportselfgetreturninitdef
1条回答
网友
1楼 · 发布于 2024-10-04 01:33:34

您正在使用Python3。做

from .Courses import Courses
from .Fridge import Fridge

Python 2会在同一个目录中查找Courses模块,但是Python 3会在站点包中查找Courses模块——显然,它不在那里。

p.S.“Phython”-听起来很有趣;)

相关问题 更多 >