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
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 338 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 443 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 429 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 298 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 507 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 576 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 464 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 416 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 339 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 293 Feb-10-2024, 06:18 PM
Last Post: Jotatochips

Forum Jump:

User Panel Messages

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