I have following folder structure
Tool -> Tool folder have following three subfolders
inputfiles
outputfiles
soucefiles
sourcefiles have following files
__init__.py
main.py
ParseLogFile.py
Following are contents of ParseLogFile.py
class ParseLogFile:
# initialize file name
def __init__(self, filename):
self.filename = filename
return
def ParseLogFileFunc(self):
print("ParseLogFileFunc called " + self.filename
)
Following are contents of main.py
from sourcefiles.ParseLogFile import ParseLogFile
parseFile = ParseLogFile("logfile")
parseFile.ParseLogFileFunc()
If i use following then it is working
from ParseLogFile import ParseLogFile
parseFile = ParseLogFile("logfile")
parseFile.ParseLogFileFunc()
but I am trying to understand absolute and relative paths for import
Question 1:
When i gave absolute path like above i am getting followoing error
from sourcefiles.ParseLogFile import ParseLogFile
If i run above file i.e., main.py i am getting following error.
ModuleNotFoundError: No module named 'sourcefiles'
How do we rectify this error using abolute path?
Question 2:If i use relative path
from .ParseLogFile import ParseLogFile
parseFile = ParseLogFile("logfile")
parseFile.ParseLogFileFunc()
If i run above file i.e., main.py i am getting following error.
ModuleNotFoundError: No module named '__main__.ParseLogFile'; '__main__' is not a package
Question 3: How do open a file by providing relate path in python for example in my case input file in "inputfiles" folder.
I am new to python and trying to learn python in realistic way used in real projects. Kindly help. I am using python 3.x and spyder tool of anaconda for development