0

I have two scripts, one calling the other in Python 27. The first script, Script1.py, contains some conditional statements. Then I have a second script, Script2.py that calls the first script and passes an argument to the function, func1 imported from the first script.

However I then get an error when running the second script that the variables in func1 are not defined. Why is this? What should I do to resolve?

Thanks

Script1.py:

def func1(var):

    if var == '1':

        test1 = 'a'
        test2 = 'b'
        test3 = 'c'

    if var == '2':

        test1 = 'd'
        test2 = 'e'
        test3 = 'f'

Script2.py:

from Script1 import func1

func1('1')

print test1, test2, test3

func1('2')

print test1, test2, test3


Traceback (most recent call last):
  File "G:/Python27/Script2.py", line 5, in <module>
    print test1, test2, test3
NameError: name 'test1' is not defined
10
  • if and a single = don't go together. Commented Apr 21, 2019 at 13:35
  • sorry, will amend...was a typo Commented Apr 21, 2019 at 13:36
  • 1
    That will not work because the scope of those variables are local to the function. Commented Apr 21, 2019 at 13:38
  • 4
    your function should probably return test1, test2, test3; and then you could call it this way: test1, test2, test3 = func1('1'). Commented Apr 21, 2019 at 13:38
  • i figured the variables were local to that function, but how do I call them from my second script? i know you can do 'from function import var', however the issue here is the function in script one is parameterised and I am therefore unsure how to call the variables in func1 based on this conditional input... Commented Apr 21, 2019 at 13:40

3 Answers 3

1
def func1(var):

    if var == '1':
        test1 = 'a'
        test2 = 'b'
        test3 = 'c'

    elif var == '2':
        test1 = 'd'
        test2 = 'e'
        test3 = 'f'

    # to catch error when different argument is passed
    else:
        test1 = test2 = test3 = None

    return test1, test2, test3 # return the variables, so they can be used outside

and:

from Script1 import func1

test1, test2, test3 = func1('1')

print test1, test2, test3

test1, test2, test3 = func1('2')

print test1, test2, test3
Sign up to request clarification or add additional context in comments.

3 Comments

this gives an error of: Traceback (most recent call last): File "G:/Python27/Script2.py", line 3, in <module> test1, test2, test3 = func1('1') TypeError: 'NoneType' object is not iterable
sorry, there was a space too much. should be fine now.
thank you. this now works perfectly in my actual code as well as the simple scenario example given in the question. I have also learned a valuable lesson on my python journey.
0

To simplify your code, you can define your func1 as follows, note that I can return the values directly, and I do not need to assign them to variables

def func1(var):

    if var == '1':
       return 'a', 'b', 'c'
    elif var == '2':
       return 'd', 'e', 'f'

Then to read those values, you read the values in a variable, and print them

from Script1 import func1
test1, test2, test3 = func1('1')
print test1, test2, test3
#a b c
test1, test2, test3 = func1('2')
print test1, test2, test3
#d e f

To go one step further, you can also assign them to a list, and print as well

print(func1('1'))
print(func1('2'))
#('a', 'b', 'c')
#('d', 'e', 'f')

Comments

0

in script1.py

T = {'test1':'', 'test2':'', 'test3':''}

def func1(var):

    if var == '1':

        T['test1'] = 'a'
        T['test2'] = 'b'
        T['test3'] = 'c'

    if var == '2':

        T['test1'] = 'd'
        T['test2'] = 'e'
        T['test3'] = 'f'

in script2.py

from script1 import (T, func1)


func1('1')

print T['test3'], T['test2'], T['test1']

func1('2')

print T['test3'], T['test2'], T['test1']

Comments

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.