[英]ImportError: No module named ' ' while Import class in the __init__.py file Python
我是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());
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
這些文件存放在Kitchen是包
import Kitchen
執行此命令時,我收到以下錯誤
回溯(最近一次調用最后一次):文件“”,第1行,導入廚房文件“E:\\ Mani \\ Learnings \\ Phython \\ Kitchen__init __。py”,第1行,來自課程導入課程ImportError:沒有名為'Courses'的模塊
請幫我解決這個問題,也請告訴我出錯的地方
你正在使用Python 3.做
from .Courses import Courses
from .Fridge import Fridge
Python 2會在同一個目錄中尋找Courses
模塊,但是Python 3在站點包中尋找Courses
模塊 - 顯然,它並不存在。
PS“Phython” - 聽起來很有趣;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.