0

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.

7
  • Can you show the code for ReadBugXML and __init__.py? Commented Jul 29, 2019 at 17:25
  • Sorry. Fixed the code positioning. Commented Jul 29, 2019 at 17:25
  • I think you need from .code import * (notice the dot in front of code) for importing from a local folder. Commented Jul 29, 2019 at 17:26
  • @clockelliptic My `__init__.py is empty Commented Jul 29, 2019 at 17:31
  • 1
    you need to do something to the effect of from code.ReadBugXML import *, not, by convention, module name should be lower-case, so readbugxml.py. Also note, using starred imports here is considered bad practice. Commented Jul 29, 2019 at 17:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.