ImportError:在__init__.py文件Python中导入类时没有名为''的模块

Tha*_*amy 5 python import

我是python编程的新手.我创建了一个名为kitchen的包.我想通过__init__.py文件导入一个类文件.

我是python版本:3.3.2

OS平台:windows

Fridge.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());
Run Code Online (Sandbox Code Playgroud)

Courses.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());
Run Code Online (Sandbox Code Playgroud)

__init__.py

from Courses import Courses
from Fridge import Fridge
Run Code Online (Sandbox Code Playgroud)

这些文件存放在Kitchen是

import Kitchen
Run Code Online (Sandbox Code Playgroud)

执行此命令时

我收到了以下错误

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());
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题,也请告诉我哪里出错了

war*_*iuc 18

你正在使用Python 3.做

from .Courses import Courses
from .Fridge import Fridge
Run Code Online (Sandbox Code Playgroud)

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

PS"Phython" - 听起来很有趣;)

  • @EduardoReis这是一个长期存在的问题,已在Python 3中修复:https://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports (2认同)