I have a class ReadBugXML in a folder called code and I added a __init__.py to make the code as a package. main.py and code are in the root folder. My main file looks like this.
import sys
from code import *
def main():
bug_report = ReadBugXML('bugreports.xml')
report, structure = bug_reports.read_bugs()
print(report[0])
if __name__ == "__main__":
main()
This is how my code\ReadBugXML.py look like
import os
class ReadBugXML:
def __init__(self, filename):
curr_dir = os.getcwd()
data_file_path = os.path.join(curr_dir, 'data', filename)
try:
self.bug_file = open(data_file_path, encoding='utf8')
except IOError:
print('The file is missing!')
def read_bugs(self):
# read the xml and return a list
I have tried different import statements but didn't work out.
I expected that the object will be created. But it doesn't create any. I have tested the code within the ReadBugXML.py by creating an object and calling the function. It did work. But calling in the main seems to be the problem. I don't know how to fix the import for user-defined classes.
Instead, I am getting a pylint error 'Undefined variable ReadBugXML' in VSCode. Running the main file gives NameError: name ReadBugXML is not defined.
Note: I am using VSCode (win 10) as an editor and python 3.6 is the interpreter.
ReadBugXMLand__init__.py?from .code import *(notice the dot in front ofcode) for importing from a local folder.from code.ReadBugXML import *, not, by convention, module name should be lower-case, soreadbugxml.py. Also note, using starred imports here is considered bad practice.