6

我是 python 编程的新手。我创建了一个名为 kitchen 的包。我想通过文件导入一个类__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

这些是文件位于 Kitchen 是

import Kitchen

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

回溯(最后一次调用):文件“”,第 1 行,在导入厨房文件“E:\Mani\Learnings\Phython\Kitchen__init__.py”,第 1 行,从课程导入课程 ImportError:没有名为“课程”的模块

请帮助我如何处理这个问题,也请让我知道我哪里出错了

4

1 回答 1

19

你正在使用 Python 3。做

from .Courses import Courses
from .Fridge import Fridge

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

PS“Phython” - 听起来很有趣;)

于 2013-07-02T11:30:19.927 回答