简体   繁体   中英

Python ImportError: No module named 'path'

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

I am getting:

ImportError: No module named path

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

os.path is working in all other classes in my project. Can someone point me what mistake i am doing?

I moved this file from one directory to another. Apart from it, there is no difference between this class and other classes.

import os should work fine instead

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

as an asside your method could be

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

I had the package name in the project as 'Lib' and moved the Testdata module into the Lib. I guess Python didn't like the package name. Renamed it to Library and now its working. The error is not related to the import statement. Both import os.path AND from os import path works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM