2013-05-13 142 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导入路径工作正常。

相关问题