6

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

3
  • 4
    I know you're new to Python, but a) don't check types b) don't use mutable default arguments c) use underscore_separated format for names d) no need for return unless you're returning a value e) no semicolons at end of lines f) consider whether you really need classes at all. Commented Jul 2, 2013 at 11:35
  • @DanielRoseman: Thanks for the valuable suggestions Commented Jul 2, 2013 at 11:38
  • @DanielRoseman > don't check types < what about using assert isisntance(items, dict) instead of if type(items) != type({})? Commented Jul 2, 2013 at 11:56

1 Answer 1

19

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 ;)

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the reply. Let me check
Also check PEP8 and name modules in lower_case
Thank you so much! I spent hours until get to the solution (your answer)... I wonder why such details like this had to change from one version to another. T.T
@EduardoReis this was a long standing issue which was fixed in Python 3: python.org/dev/peps/pep-0328/#rationale-for-absolute-imports
@BenjaminR yes, IMO Python docs suck. It would be nice to comments sections like in PHP with notable examples/remarks from users.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.