2013-07-02 14 views
5

Sono nuovo alla programmazione Python. Ho creato un pacchetto chiamato cucina. Voglio importare un file di classe attraverso il file __init__.py.ImportError: nessun modulo chiamato '' while Importa classe nel file __init__.py Python

Sono versione di Python: piattaforma 3.3.2

SO: Windows

Fridge.py presentare

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

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 

Si tratta di file è risiedeva a Kitchen è il pacchetto

import Kitchen 

Durante l'esecuzione di questo comando

sto ottenendo seguente errore

Traceback (most recent call last): 
    File "<pyshell#50>", line 1, in <module> 
    import Kitchen 
    File "E:\Mani\Learnings\Phython\Kitchen\__init__.py", line 1, in <module> 
    from Courses import Courses 
ImportError: No module named 'Courses' 

Please help me come gestire questo e per favore fatemi sapere dove sono andato storto

+3

So che sei nuovo in Python, ma a) non controllare i tipi b) non utilizzare argomenti di default mutabili c) usa il formato underscore_separated per i nomi d) non c'è bisogno di ' return' a meno che tu non stia restituendo un valore e) no punto e virgola alla fine delle righe f) considera se hai davvero bisogno di classi. –

+0

@DanielRoseman: Grazie per i preziosi suggerimenti –

+0

@DanielRoseman> non controllare i tipi warvariuc

risposta

16

Si sta utilizzando Python 3. Fare

from .Courses import Courses 
from .Fridge import Fridge 

Python 2 cercherebbe Courses modulo nello stesso dir, ma Python 3 sguardi per Courses modulo in pacchetti del sito - e, ovviamente, non è lì.

P.S. "Phython" - sembra interessante;)

+0

Grazie per la risposta. Fammi controllare –

+0

grazie al fatto che funziona ora –

+0

Controlla anche [PEP8] (http://www.python.org/dev/peps/pep-0008/) e chiama i moduli in lower_case – warvariuc

Problemi correlati