Python Forum
How to call one python script and use its output in another python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call one python script and use its output in another python script
#1
Hi,

I have two python scripts and I want to call 1st python script and use the output of this script in another python script.

For example:
I have two python scripts t1.py and t2.py

t1.py
a = 9
print a
t2.py
import t1
b = str(a)
print b
When I ran python t2.py, it is resulting in the output of t1.py and getting the below error:
9
Traceback (most recent call last):
File "t2.py", line 2, in <module>
b = str(a)
NameError: name 'a' is not defined

Please help
Reply
#2
write t2 this way:
import t1

b = str(t1.a)
print(b)
As an FYI, you should refrain from using single letter variables and use something meaningful instead.
And if you are just starting, you should start off with the latest version (3.6.5) of python.
Reply
#3
In addition to Larz60+ advice - you may want to change t1.py as

a = 9
if __name__ == '__main__':
    print a
this way it will not execute the print statement in t1.py when you import it, yet you will access a in t2.py
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks All
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script from remote to server invisiblemind 4 620 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 885 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 523 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 2 1,202 Feb-24-2025, 05:12 AM
Last Post: from1991
  Detect if another copy of a script is running from within the script gw1500se 4 1,056 Jan-31-2025, 11:30 PM
Last Post: Skaperen
  pass arguments from bat file to pyhon script from application absolut 2 926 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 1,047 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,096 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,183 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Help for Tiktok Script Jasson187512 0 724 Oct-09-2024, 08:42 AM
Last Post: Jasson187512

Forum Jump:

User Panel Messages

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