0

When I try to import a module to run it with specific inputs, the beginning part of the python script i'm calling gets run too which asks me for user input. I am not allowed to change the contents of that file in any way. How can I suppress that beginning part from running and just test the function I want to test in isolation?

module_path = os.path.join(root_dir, _dir, filename)  
mod = imp.load_source(modified_file, module_path)

point_counter = 0
if(mod.Function(2, 5) != True):
   point_counter+=1
if(mod.Function(2, 4) != False):
   point_counter+=1
segfault
  • 43
  • 1
  • 10
  • 1
    Not sure what you're trying to do there but might `from module import function` be what you're looking for ? – Trolldejo May 17 '17 at 07:58
  • @Trolldejo That is basically what I am doing with os, but the problem is that since the parts of the program that I am trying to run are not in a main function, when I try to call a specific function in the program, ie. mod.Function(2, 5), the parts not in the function are also called which ask me for user input. – segfault May 17 '17 at 08:06
  • concidering the use of imp.load_source, I guess you do are using python2. May you confirm ? If so please remove the python3 tag – Trolldejo May 17 '17 at 08:40
  • @Trolldejo No I use python3. – segfault May 17 '17 at 08:41
  • ok, my bad ;) may you give me the content of your variables so I can test it on my terminalt to make sure my solution works for you ? – Trolldejo May 17 '17 at 08:46
  • root_dir = /.root/ _dir = /path_to_directory_I_want_to_get_program_from/ filename = _file.py modified_file = file – segfault May 17 '17 at 15:58
  • you can't import part of a module without running through it. The "filename" isn't written the good way. What you may do is copy past it so you can edit the code maybe..? – Trolldejo May 18 '17 at 12:38
  • http://stackoverflow.com/questions/44046615/how-to-import-function-from-a-module-given-absolute-path/44049215#44049215 – Trolldejo May 18 '17 at 13:33

0 Answers0