PYTHON call a function after import module finishes - python

I want to execute a function once a module is imported. I know I can just call the function directly at the end of the module to achieve this.
For eg..
# Inside File Test.py
import something
from test import sayhi
# Lots of functions and classes are defined here
# End of the module
sayhi()
But, Since there will be several people changing the file continuously, I do not want to put sayhi() at the end of the file but want sayhi() to be called once the loading finishes. is there a hook in python that executes once the import module statement finishes? I am looking for something like sys.settrace with the difference being it should be called only after the import finishes.

Related

How do I get two modules to run after each other?

I'm trying to import two modules so that they run after each other. Here is my code:
from Game import *
main()
from Typing_Question_Screen import *
When I run my code, the Game module loads first but if I close the file, the Typing_Question_Screen module is loaded too. But once the Game module finishes, nothing loads next.
How can I run these two right after each other so that when the Game file ends, the Typing_Question_Screen loads too? Thank you.
From my understanding you have two files:
file1.py
file2.py
If you want to run file.1py and then run file2.py without having to manually do it, there are multiple ways. I would recommend creating a batch.
So you have your two .py files. You will want to create a new file. Don't worry about the extension. We will take care of that later. Navigate to your terminal at the bottom and type in where python. That is the first thing you will put in the double quotes. The next is where your file1.py file is located. You will do the same for file2.py indicated in picture 5. You can use the pause function to pause the process. So after file1.py and ran you will be promted to start file2.py. If you don't want to be prompted, then just remove the pause. You then need to navigate to your file explore to where the batch file was created. In my case it is here (picture 6). Right click on the batch file and select rename. Add .bat at the end. That will convert it to the batch file. Then when you run it (doubleclick). The two .py files that we set up in the batch will run!
The statements in the global scope of imported modules are executed when they are imported. So, you may consider adding function calls or other desired statements in the global scope. Let me give an example:
Game.py:
def main():
print("game")
main()
Typing_Question_Screen.py:
def test():
print("tqs")
test()
The importing file:
from Game import *
from Typing_Question_Screen import *
print("import complete")
The output of this file would be:
game
tqs
import complete
Note that you can check whether the module is being executed independently. (Please keep in mind that usually the opposite of this behavior is used, and generally imports are not expected to execute code by themselves.)
For example, the below implementation calls main only if the module is being imported:
def test():
print("tqs")
if __name__ != __main__:
test()
__name__, a special variable of the module set by the interpreter, is __main__ when the module is run without being imported.
Also, I would avoid importing with *. Moreover, the imports should be at the top of the importing file, separated from other statements and definitions. See: PEP8 on importing
I think you may modify your code as:
import Game
import Typing_Question_Screen
Game.main()

from script import function runs everything? [duplicate]

I'm new to python and doing an assignment. It's meant to be done with linux but as I'm doing it by myself on my own computer I'm doing it on windows.
I've been trying to do this test system that we use looking like this:
>>> import file
>>> file.function(x)
"Answer that we want"
Then we run it through the linux terminal. I've been trying to create my own way of doing this by making a test file which imports the file and runs the function. But then on the other hand of just running the function it runs the whole script. Even though it's never been called to do that.
Import file
file.function(x)
That's pretty much what I've been doing but it runs the whole "file". I've also tried from file import function; it does the same.
What kind of script can I use to script the "answer that I want" for the testfile? As when we run in through linux terminal it says if it has failed or scored.
importing a file is equivalent to running it.
When you import a file (module), a new module object is created, and upon executing the module, every new identifier is put into the object as an attribute.
So if you don't want the module to do anything upon importing, rewrite it so it only has assignments and function definitions.
If you want it to run something only when invoked directly, you can do
A = whatever
def b():
...
if __name__ == '__main__'
# write code to be executed only on direct execution, but not on import
# This is because direct execution assigns `'__main__'` to `__name__` while import of any way assigns the name under which it is imported.
This holds no matter if you do import module or from module import function, as these do the same. Only the final assignment is different:
import module does:
Check sys.modules, and if the module name isn't contained there, import it.
Assign the identifier module to the module object.
from module import function does
Check sys.modules, and if the module name isn't contained there, import it. (Same step as above).
Assign the identifier function to the module object's attribute function.
You can check if the module is imported or executed with the __name__ attribute. If the script is executed the attribute is '__main__'.
It is also good style to define a main function that contains the code that should be executed.
def main()
# do something
pass
if __name__ == '__main__'
main()

How to run script with a function? (repeatedly)

I have a Python program where a function imports another script and runs it. But the script gets run only the first time the function is called.
def Open_Generator(event):
import PasswordGenerator
Any tips?
*The function is called using a button in a tkinter window
This is by design. You should only import a module once. Trying to import a module more than once will cause Python to re-fetch the module object from the cache, but this won't cause the module's code to execute a second time.
Most well-designed modules won't do anything right away when you import them, or at least won't do anything obviously visible. Generally, if you want a module to do work, you need to call one of its functions.
I'm guessing your PasswordGenerator module has some code at the file-level scope. In other words, it has code that isn't inside a function. Try to move that code into a function. Then you can call that function from Open_Generator.
import PasswordGenerator
def Open_Generator(event):
my_password = PasswordGenerator.generate_password()

When importing a function it runs the whole script?

I'm new to python and doing an assignment. It's meant to be done with linux but as I'm doing it by myself on my own computer I'm doing it on windows.
I've been trying to do this test system that we use looking like this:
>>> import file
>>> file.function(x)
"Answer that we want"
Then we run it through the linux terminal. I've been trying to create my own way of doing this by making a test file which imports the file and runs the function. But then on the other hand of just running the function it runs the whole script. Even though it's never been called to do that.
Import file
file.function(x)
That's pretty much what I've been doing but it runs the whole "file". I've also tried from file import function; it does the same.
What kind of script can I use to script the "answer that I want" for the testfile? As when we run in through linux terminal it says if it has failed or scored.
importing a file is equivalent to running it.
When you import a file (module), a new module object is created, and upon executing the module, every new identifier is put into the object as an attribute.
So if you don't want the module to do anything upon importing, rewrite it so it only has assignments and function definitions.
If you want it to run something only when invoked directly, you can do
A = whatever
def b():
...
if __name__ == '__main__'
# write code to be executed only on direct execution, but not on import
# This is because direct execution assigns `'__main__'` to `__name__` while import of any way assigns the name under which it is imported.
This holds no matter if you do import module or from module import function, as these do the same. Only the final assignment is different:
import module does:
Check sys.modules, and if the module name isn't contained there, import it.
Assign the identifier module to the module object.
from module import function does
Check sys.modules, and if the module name isn't contained there, import it. (Same step as above).
Assign the identifier function to the module object's attribute function.
You can check if the module is imported or executed with the __name__ attribute. If the script is executed the attribute is '__main__'.
It is also good style to define a main function that contains the code that should be executed.
def main()
# do something
pass
if __name__ == '__main__'
main()

WLST scripting and importing self-made module

I am trying to write a WLST script.
As I found that I always repeat doing similar setup, I tried to make some util functions to ease my script writing.
Later when I tried to pull those functions to an external .py as a module, I failed to do so:
assume I have a main script (domain_config.py), and the util function script (wlst_util.py)
Here is what I put in domain_config.py:
import wlst_util import *
loadProperties('domain.properties')
....
create_jms_conn_factory(....);
First it complains for my delcaration in the wlst_util.py for the method:
create_jms_conn_factory(...., is_xa=False)
it complains "NameError: False".
ok, then I remove the default param, then it complains for those cd() function (provided by WLST).
Then I tried to do "from wl import *" in wlst_util.py, the script failed at loadProperties line (NullPointerException).
I tried to put the import after loadProperties, then the cmo variable in my main script become None...
What is the right way I should do just for pulling those util function to a separate file?..
Thanks

Categories

Resources