2013-05-13 143 views
2
''' Data class''' 

import os.path 
from random import Random 

class TestData(object, Random): 

    def FetchDataFromFile(self, filename): 
     """ Open the file as read only """ 
     myfile = open(os.path.join(os.getcwd(),filename), 'r') 
     """ read the information in the file """ 
     lines = myfile.read() 
     ''' Remove the header as this will not be used ''' 
     header = lines[0] 
     lines.remove(header) 
     return lines 

我越來越:的Python導入錯誤:沒有模塊名爲 '路徑'

ImportError: No module named path

File "pyclasspath/Lib/Testdata.py", line 2, in

os.path中工作的所有其他類在我的項目。有人能指出我在做什麼錯誤嗎?

我把這個文件從一個目錄移到另一個目錄。除此之外,這門課與其他課程沒有區別。

+0

你可以'從os導入路徑',但我認爲首選的方法是'import os',然後在腳本中使用'os.path.method_name()'。如果他使用'從OS進口path' – squiguy 2013-05-13 05:31:19

+0

@squiguy,他需要用'path'上獲得10K代表,而不是'os.path' – 2013-05-13 06:02:27

回答

3

import os應該可以正常工作,而不是

import os 
from random import Random 

class TestData(object, Random): 

    def FetchDataFromFile(self, filename): 
     """ Open the file as read only """ 
     myfile = open(os.path.join(os.getcwd(),filename), 'r') 
     """ read the information in the file """ 
     lines = myfile.read() 
     ''' Remove the header as this will not be used ''' 
     header = lines[0] 
     lines.remove(header) 
     return lines 

爲asside你的方法可能是

def FetchDataFromFile(self, filename): 
     """ Open the file as read only """ 
     return list(open(os.path.join(os.getcwd(),filename), 'r'))[1:] 
+1

祝賀。 – squiguy 2013-05-13 05:41:52

+0

好的。我發現了錯誤。目標文件夾名稱是Lib。我不得不將它改爲其他名稱,庫,並且它再次開始工作。感謝您的建議。 – Loganswamy 2013-05-13 05:55:15

+0

謝謝@squiguy :) – 2013-05-13 15:19:36

2

我在該項目作爲「庫」有包名和移動TESTDATA模塊到lib 。 我猜的Python不喜歡包的名稱。將其重命名爲Library,現在正在工作。該錯誤與導入語句無關。兩個導入os.path和從os導入路徑工作正常。

相關問題