Python Forum
How to call a variable declared in a function in another python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call a variable declared in a function in another python script
#1
Hi,

I had two python scripts r1.py and r2.py
In r1.py I used a variable named "test" in a function and I want to use this in r2.py.
I tried but not able to get it.
Please help

r1.py
test = 10
def f1():
	global test
	test = test + 100
r2.py
import r1
a = test + 100
print a
output: When I ran python r2.py
Output:
Traceback (most recent call last): File "r2.py", line 2, in <module> a = test + 100 NameError: name 'test' is not defined
Reply
#2
in r2.py
change:
a = test + 100
to:
a = r1.test + 100
It's better to do this without use of globals.
In addition, you should use meaningful names, not single letters.
Reply
#3
Please don't send private messages. The forum is for all to learn from.
I am re-posting your pm here:
............................................
Hi Larz,

Thanks and my problem is that I want to use the variable declared in a function in another python script.
I am able to call the variables declared globally.

r1.py
def f1():
    test = 100
r2.py
import r1
a = r1.test
print a
here in r1.py I declared a variable "test" and I want to use this in r2.py
............................................

You could do this better by using a class:

r1.py
class r1:
    def __init__(self):
        self.test = None

    def f1(self):
        self.test = 100
r2.py
import r1

# create instance of r1
myr1 = r1.r1()

# execute r1 function f1
myr1.f1()

# Create new variable a which is equal to myr1.test
a = myr1.test + 100
print(a)
results:
Output:
200
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 719 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Variable for the value element in the index function?? Learner1 8 629 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,276 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 921 Aug-07-2023, 05:58 PM
Last Post: Karp
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,181 Jun-29-2023, 11:57 AM
Last Post: gologica
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 787 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Trouble with threading and reading variable from a different script Lembas 14 3,009 Apr-26-2023, 11:21 PM
Last Post: Lembas
  Retrieve variable from function labgoggles 2 1,037 Jul-01-2022, 07:23 PM
Last Post: labgoggles
Question How can I import a variable from another script without executing it ThomasFab 12 7,743 May-06-2022, 03:21 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020