Python Forum
Grabbing a value from one python script into another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grabbing a value from one python script into another
#2
# Accelerator.py
revs_pc = 999
if __name__ == '__main__':
    print(f"Revs = {revs_pc}")
# bar.py
import Accelerator
import math

def foo(arg):
    value = math.factorial(arg) + Accelerator.revs_pc
    return value

if __name__ == '__main__':
    print(foo(10))
Output:
3629799
So it's a good practice to use if __name__ == '__main__',then dos code after that do not execute when import it.
Have to call Accelerator.revs_pc to get 999.
Also look that i use f-string to avoid the not so nice way print("Revs = " + str(int(revsPC))).
Reply


Messages In This Thread
RE: Grabbing a value from one python script into another - by snippsat - May-24-2019, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,828 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 6,223 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,434 May-28-2020, 05:27 PM
Last Post: micseydel
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 4,295 Apr-10-2020, 02:39 PM
Last Post: buran
  Grabbing a Subset of a String acemurdoc 3 2,728 Jun-18-2019, 04:57 PM
Last Post: perfringo
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,736 May-11-2019, 08:12 PM
Last Post: keames
  Grabbing questions from a quiz app CodingUnicorn 0 2,043 Mar-05-2019, 11:26 PM
Last Post: CodingUnicorn
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,989 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 6,372 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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