Python Forum
Close script from another script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Close script from another script
#5
In Python each code file is a "module". Modules each have a namespace. You can access these variables if you have access to their namespace. When you do "import module" you are gaining access to the module's namespace.

I have three files;
test1.py:
x = 10

def printx():
    print(x)
test2.py
import test1

def printx():
    print(test1.x)

def setx(value):
    test1.x = value
test3.py
import math
import test1
import test2
from test1 import x

print(x, test1.x)
test1.printx()
test2.printx()

print('test2.setx(42)')
test2.setx(42)
print(x, test1.x)
test1.printx()
test2.printx()

print('test1.x = PI')
test1.x = 'PI'
print(x, test1.x)
test1.printx()
test2.printx()

print('x = math.pi')
test1.x = math.pi
print(x, test1.x)
test1.printx()
test2.printx()
When I execute test3.py I get this:
Output:
========== RESTART: C:\Users\hystadd\Documents\python\sandbox\junk.py ========== 10 10 10 10 test2.setx(42) 10 42 42 42 test1.x = PI 10 PI PI PI x = math.pi 10 3.141592653589793 3.141592653589793 3.141592653589793
Notice that I can set the value of test1.py from modules test1, test2 or test3. When I change the value of test1.x the change can be seen in test1, test2 and test3.

Also notice that "from test1 import x' does not work the same as explicitly using the namespace (test1.x). It would appear that "from test1 import x" creates a new variable in the current module's namespace. This was verified by checking the variable id.
Output:
>>> id(x) 140721929705408 >>> id(test1.x) 2714390832848
This could be an issue if you use from..import.. and expect to be able to change an immutable variable.
Larz60+ likes this post
Reply


Messages In This Thread
Close script from another script - by ilgrabbo - Jan-20-2021, 10:00 AM
RE: Close script from another script - by ndc85430 - Jan-20-2021, 12:50 PM
RE: Close script from another script - by ilgrabbo - Jan-20-2021, 05:19 PM
RE: Close script from another script - by deanhystad - Jan-20-2021, 07:02 PM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 10:00 AM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 03:11 PM
RE: Close script from another script - by ilgrabbo - Jan-21-2021, 04:45 PM
RE: Close script from another script - by ilgrabbo - Jan-22-2021, 09:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Running script from remote to server invisiblemind 4 950 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 1,357 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 695 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 1 1,402 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,300 Jan-31-2025, 11:30 PM
Last Post: Skaperen
  pass arguments from bat file to pyhon script from application absolut 2 1,516 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 1,436 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,353 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,409 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Help for Tiktok Script Jasson187512 0 848 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