[英]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
我正進入(狀態:
ImportError:沒有名為路徑的模塊
文件“ pyclasspath /Lib/Testdata.py”,第2行,在
os.path在我項目的所有其他類中工作。 有人可以指出我在做什么錯嗎?
我將此文件從一個目錄移動到另一個目錄。 除此之外,該類與其他類沒有區別。
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:]
我在項目中將包名稱命名為“ Lib”,並將Testdata模塊移至Lib中。 我想Python不喜歡包名。 將其重命名為Library,現在可以正常工作了。 該錯誤與import語句無關。 從os導入路徑和從os導入路徑都可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.