Python ImportError:没有名为'path'的模块

时间:2013-05-13 05:28:45

标签: python

''' 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

我得到了:

  

ImportError:没有名为path

的模块      

文件“ pyclasspath /Lib/Testdata.py”,第2行,

os.path正在我项目中的所有其他类中工作。有人能指出我在做什么错误吗?

我将此文件从一个目录移动到另一个目录。除此之外,这个班级和其他班级没有区别。

2 个答案:

答案 0 :(得分: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

作为帮助你的方法可能

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

答案 1 :(得分:2)

我将项目中的包名称设为'Lib',并将Testdata模块移动到Lib中。 我猜Python不喜欢包名。将其重命名为Library,现在正在工作。该错误与import语句无关。从os导入路径导入os.path AND都可以正常工作。