Bit concerned I might be missing something obvious but I seem to be having trouble passing a variable from one program to another: Test1.py to Test2.py:
Test1.py:
myVar = True
print("1myVar:", myVar)
import Test2
Test2.py
print("2myVar:", myVar) # <- fails, myVar not defined
def myFunc():
print("3myVar:", myVar)
myFunc()
You’re not passing a variable, you’re just importing another module, which can’t find the variable because it’s not in that module. The variable exists only in the original module.
You could try passing in the variable after the import by having the original module put it in there, or, better yet, pass it as an argument to a function from the imported module:
Although this does work, it feels like one heck of a workaround. Is there really no way to have global variables in Python which are available to all modules, What about if we wanted to have a file of Constants available to all code in an application?
from Test1 import myVar
from Test2 import myFunc
print("3myVar:", myVar)
myFunc()
We don’t generally call this “passing” a value. You pass arguments to a function, but that usually lives only as long as the call. Exporting, publishing, … something like that.
Generally you avoid changing the value of a name from another module because when you find in debugging that it is something surprising, it is difficult to work out who changed it.
Why do you need to do that?
From a module, you can import scalars, functions and classes.
If multiple scripts need to modify values, you could use a JSON file that is read by all SCRs.
But that is the wrong way around isn’t it? Test1.py is the ‘main’ program, it is Test1.py which runs Test2.py. In your example Test2.py would never get called.
Because parent.py needs to import child.py so that a class method in parent.py can call a class method in child.py But child.py has some code in it’s _init _ which needs access to some ‘global’ variables from parent.py. There is no method available in Python to pass parameters when import ing the child.py module.
I don’t know. Which of these is the program and which the library?
OK, I think I see that what you want is to treat Test2 as the library and Test1 as the client, and have the library refer to a variable in the client by name. (Like a Fortran COMMON block.)
But there is no (sensible) way to do it because it is a terrible design. It makes the library Test2 unusable with anything but the original program Test1, or maybe some other program written with exactly the right variable names. In which case, they might as well be in the same source file.
If you want the functions in a library to modify data in your program, then your first approach should be to pass the values into the functions as arguments, and receive the new values back as returned results.
If your main program has complex data to share, then you can pass in an instance of that structure, and have the library functions manipulate that. The structure could be a built-in, or one defined by the library as a class. The client holds an instance. The library doesn’t keep a reference to it outside the functions that manipulate it.
I feel you probably know this, but here’s a worked example anyway (pretending the structure is complex enough to warrant a class).
Well the overall design is a Web Server Gateway Interface:
WSGI.py is the WSGI.Application with associated classes and methods. This calls:
Page.py which contains the associated page serving classes and methods.
Just as an example lets look at Logging: error logs are started in WSGI.py but page_process logs are in Page.py. Various logging parameters are set in WSGI.py but I also want them available in Page.py rather than having to repeat them.
Could all exist in one overall module but it may be likely that a different Page.py will exist for each customer application.