Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

import

I am new to python programming. I have create package called kitchen. I want import a class file through __init__.py file.

I am python version : 3.3.2

OS platform : 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());

Courses.py file

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

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'

Please help me how to handle this and also please let me know where I went wrong

like image 496
Thangamani Palanisamy Avatar asked Jul 02 '13 11:07

Thangamani Palanisamy


1 Answers

You are using Python 3. Do

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.

P.S. "Phython" - sounds interesting ;)

like image 76
warvariuc Avatar answered Nov 15 '22 06:11

warvariuc