How to reload the main file in Python console? - python

I'm doing updates to other modules imported in my main file, and I can reload them on the fly in the console and get the new behaviour immediately via
import importlib
importlib.reload(module_name)
How can I do this for the main file?
I tried __name__ but did not work.
PS: I have gigantic datasets and I hate running again the main file after tiny changes, takes forever.

You should put your code in the main file into a main function and add this to the end:
if __name__ == "__main__":
main()
This will let you reload the module while also letting you run it normally. However, after you reload, you have to call module.main().

Related

How to execute another Python file from current script?

So I have a login script called login.py. When user log in, script will create a user session and execute main.py and close login.py. How to do this?
I'm a little confused by what you need help with but try this:
from fileName import function_you_want
Ideally you don't want a script to run on an import. It can cause some unexpected things to happen. At the bottom of main.py I would use this:
if __name__ == "__main__":
call_your_function()
This way you can do tests by running the script directly or calling a specific method from another py file without running the script.
What you can do is import that file main.py like import main. If you want everything from that file, from main import *
Then, after the user logins, destroy your main window using <Tk>.destroy() and then call your function.

Importing variables from another Python script

Is it possible to import a Python script into a main script and then just use the variables? I don't want the other script to execute inside of the main script, just attach one of the variables...
If you can edit the script being imported, there's a solution:
Put all the executed code into a block starting with
if __name__ == '__main__':
In this way, the code in the if block above will be executed only if the script is run directly. i.e. when you run python script.py.
You can import only the variable like this:
from script import var
If you want to import the variable without going through the whole script, then as per Python's design it's not possible. import will always go through the whole script being imported, and will avoid whatever that's inside __name__ == '__main__'.
You have an option to read the script file and find the variables, then do it yourself by parsing and executing it, but IMO that's unnecessary job to do since you have import.
You can see the answers under Python: import module without executing script

Import File In Python to another File

Using Python 2.7 on Linux
i have two files one is called plot10.py & the other one is called plot10i.py and has def main() in it
with plot10.py being my main file, this is my code:
When you import a module for the first time, it gets executed. So, you need to encapsulate all your execution flow inside functions that will be called from the main program.
In fact, in plot10i.py your main is as trivial as useless: just prints hello.
You don't need to use if __name__ == '__main__' if you don't have anything to put in. But if you don't and your project is small, you can use that to include some tests. For example, I would add one to things llike datetime.strptime(x, '%d/%m/%Y %H:%M') because they are easy to mess up.

Main program variables in side programs (Python)

How do I use variables that exist in the main program in the side program? For example, if I were to have Var1 in the main program, how would I use it in the side program, how would I for example, print it?
Here's what I have right now:
#Main program
Var1 = 1
#Side program
from folder import mainprogram
print(mainprogram.Var1)
This I think would work, if it didn't run the main program when it imports it, because I have other functions being executed in it. How would I import all the main program data, but not have it execute?
The only thing I thought of was to import that specific variable from the program, but I don't know how to do it. What I have in my head is:
from folder import mainprogram
from mainprogram import Var1
But it still excecutes mainprogram.
Your approach is basically correct (except for from folder import mainprogram - that looks a bit strange, unless you want to import a function named mainprogram from a Python script named folder.py). You have also noticed that an imported module is executed on import. This is usually what you want.
But if there are parts of the module that you only want executed when it's run directy (as in python.exe mainprogram.py) but not when doing import mainprogram, then wrap those parts of the program in an if block like this:
if __name__ == "__main__":
# this code will not be run on import

python: how to tell if file executed as import vs. main script?

I'm writing a python file mylib.py
I'd like mylib.py to do something based on sys.argv if it's being executed as a script. But if it's imported from some other script, I don't want it to do that.
How can I tell if my python file is being imported or it's a main script?
(I've seen how to do this before, but I forgot.)
if __name__ == '__main__':
# this was run as a main script
Here is the documentation on __main__.
Usually this code is placed at the bottom of a module, and one common way to keep your code clean is to create a main() function that does all of the work, and only call that function inside of the conditional.
if __name__ == '__main__':
# goes here only when module is being executed directly
Packages also can contain __main__ module, which is executed when you do python -m foo (or execute zipfile containing the package).
By using (placing the statements you want to be executed only when the module is running as main, not imported)
if __name__ == "__main__":
# this was run as a main script
Generally different statements have has to be placed in this 'if' block like , module specific doctest call or print statements.The thing is by default (when running as main) the '__name__' variable is set to "__main__", and otherwise (if imported) the __name__ variable 'll get a different value, most probably the name of the module.

Categories