简体   繁体   English

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

[英]ImportError: No module named ' ' while Import class in the __init__.py file Python

I am new to python programming. 我是python编程的新手。 I have create package called kitchen. 我创建了一个名为kitchen的包。 I want import a class file through __init__.py file. 我想通过__init__.py文件导入一个类文件。

I am python version : 3.3.2 我是python版本:3.3.2

OS platform : windows OS平台:windows

Fridge.py 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());

Courses.py file 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());

__init__.py

from Courses import Courses
from Fridge import Fridge

These are files is resided at Kitchen is the package 这些文件存放在Kitchen是

import Kitchen

While executing this command I am getting following error 执行此命令时,我收到以下错误

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' 回溯(最近一次调用最后一次):文件“”,第1行,导入厨房文件“E:\\ Mani \\ Learnings \\ Phython \\ Kitchen__init __。py”,第1行,来自课程导入课程ImportError:没有名为'Courses'的模块

Please help me how to handle this and also please let me know where I went wrong 请帮我解决这个问题,也请告诉我出错的地方

You are using Python 3. Do 你正在使用Python 3.做

from .Courses import Courses
from .Fridge import Fridge

Python 2 would look for Courses module in the same dir, but Python 3 looks for Courses module in site packages - and, obviously, it's not there. Python 2会在同一个目录中寻找Courses模块,但是Python 3在站点包中寻找Courses模块 - 显然,它并不存在。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM