2

If two files have function with same name ,importing them using source(findFile()) in a script and calling it accesses the function in the file associated at the last.How to access a function in a particular file?Does squish with python support import file syntax?

Here is a reference

script_1.py

def runner1():
    test.log("Hey")

script_2.py

def runner1():
    test.log("Bye")

Script :

source(findFile("scripts","script_1.py"))
source(findFile("scripts","script_2.py"))


runner1()//function call

O/P:Bye

Note:When I import using filename it throws error as "module not present"

Torxed
  • 22,866
  • 14
  • 82
  • 131
Gunacelan M
  • 179
  • 2
  • 10

2 Answers2

2

source() causes the "symbols" (functions, variables) in the specified file to be loaded into the namespace/scope of the test.py file. This means that source() is the wrong tool for this problem.

(Using the trick shown by Orip, to assign the function to another symbol/name after the first source() I would advise against, since other code relying on the desired function to be available under the initial name would call the wrong function eventually.)

Using Python's import statement you can achieve that the functions are in separate namespaces, by treating the files as Python modules. For this to work you have to include the directory paths containing the desired files into Python's own "search path" - sys.path:

Contents of suite_mine/tst_testcase1/test.py:

# -*- coding: utf-8 -*-

import os.path
import sys

# Add path to our modules to the Python search path at runtime:
sys.path.append(os.path.dirname(findFile("scripts", "file1.py")))
sys.path.append(os.path.dirname(findFile("scripts", "file2.py")))

# Now import our modules:
import file1
import file2


def main():
    # Access functions in our modules:
    file1.do_it()
    file2.do_it()

Contents of suite_mine/tst_testcase1/file1.py:

# -*- coding: utf-8 -*-

import test


def do_it():
    test.log("file1.py")

Contents of suite_mine/tst_testcase1/file2.py:

# -*- coding: utf-8 -*-

import test


def do_it():
    test.log("file2.py")

Resulting log entries:

file1.py
file2.py
frog.ca
  • 684
  • 4
  • 8
1

When you evaluate the contents of the file one after the other:

  1. The first source() one defines a "runner1" function
  2. The second source() overrides it with a new "runner1" function

According to the squish docs you can import modules. It's possible you may need to mark the scripts/ directory as a package by creating an empty file in it called __init__.py.

You should then be able to do

import scripts.script_1
import scripts.script_2
scripts.script_1.runner1()
scripts.script_2.runner1()

or

from scripts.script_1 import runner1 as foo1
from scripts.script_2 import runner1 as foo2
foo1()
foo2()

You can even keep using source() by keeping a new reference to the first runner1 function. It's hacky as hell, though, so prefer the import solution if you can make it work.

source(findFile("scripts","script_1.py"))
foo = runner1
source(findFile("scripts","script_2.py"))

foo()     # runs runner1 from script_1
runner1() # runs runner1 from script_2
orip
  • 73,323
  • 21
  • 116
  • 148
  • 1
    I tried and observed the following ->The interpreter tries to look for the file under the directory where the current script file is placed.And if I place the file under this path,it works fine(Even without __init__.py file). ->On further R&D,I got to know about the default paths where interpreter will search for the python file ,one such being PATH variable and so I added the current directory where the file is located to the PATH variable(even with __init__.py in the dir) but got struck up with same issue. :( – Gunacelan M Oct 09 '17 at 12:54
  • 1
    Wow. You saved my day. My problem was overriding functions from surce files (using source instead of import because of other problems) and I couldn't referenced them. But overriding it's name resolved my problem. Thanks – matebende Aug 19 '20 at 10:31