-2

I have three files under the same directory, namely, main.py, Newtester.py, and fileUtility.py. In Newtester.py there is a class named Function. In main.py, there are the following codes:

from file.py import *
...
def main():
    ...
    funcs = parseFuncSpec(funcInputFile)

parseFuncSpec is defined in fileUtilities.py as:

some code to import Newtester.py
def parseFuncSpec(fName):
    curFunc = function(funcName, numTest, [], score)

Regardless of what I put in import Newtester.py, I always get an error saying "Function" (the class defined in the file "Newtester.py") is not defined. Following Python: How to import other Python files, I have attempted

import Newtester
__import__("Newtester")
exec("Newtester.py")
exec("Newtester")
import importlib
importlib.__import__("Newtester")
os.system("Newtester.py")

But none of them seemed to work. Any advice is appreciated. See https://github.com/r2dong/unitTesting if you are interested in seeing the complete files.

Rentian Dong
  • 101
  • 1
  • 8

1 Answers1

1

It's because you are not using it correctly

well when you use import statement like below only Newstester file is imported

import Newtester

hence instead of using parseFuncSpec() directly you have to use it as Newtester.parseFuncSpec()

or to use parseFuncSpec() directly you need to use below import statement:

from Newtester import parseFuncSpec
Gahan
  • 4,075
  • 4
  • 24
  • 44