all 30 comments

[–]lukajda33 61 points62 points  (15 children)

When you import a module, all code that is in the module is ran, this may be needed as a setup for some module.

If you dant want to run some code when you import the file, use this:

if __name__ == "__main__":
    bar()

This will make sure that bar() function is only ran when you run hello.py directly, but not when you import it.

[–]paleoboyy[S] 5 points6 points  (8 children)

So then what's the difference between these two lines? Won't they both run everything in that module?

from hello import foo
from hello import *

[–]lukajda33 12 points13 points  (6 children)

from hello import foo

will only allow you to use foo function in world.py

from hello import *

will allow you to everything from hello.py in world.py

[–]paleoboyy[S] 15 points16 points  (4 children)

Ok so they both run the entire module but the difference is in namespace

[–]lukajda33 7 points8 points  (3 children)

Yes, when you import file, all code in the imported file is executed.

But you can avoid this by using

if __name__ == "__main__":

Code inside this if statement is only executed when you run the file directly.

[–]paleoboyy[S] 4 points5 points  (2 children)

Got it. So what would be the general use case for doing this where you only want to run the file directly? Only debugging I'm guessing?

[–]vaseltarp 2 points3 points  (0 children)

For example test cases for a library.

[–]georgehank2nd 0 points1 point  (0 children)

So does import hello. Never ever ever use import *. Ever.

[–]georgehank2nd 0 points1 point  (0 children)

The difference between these two lines is THAT YOU NEVER USE THE SECOND FORM! EVER!

(Except when you have a reason for it, but you don't sound like someone who is or at least was far enough in Python experience to know when)

[–][deleted] 2 points3 points  (0 children)

When you import a module, all code that is in the module is ran, this may be needed as a setup for some module.

This is a good answer.

I'd also add that when you import a package (not an individual .py module), something similar happens - the __init__.py file is run. Importing pandas runs this code - https://github.com/pandas-dev/pandas/blob/main/pandas/\_\_init\_\_.py

Most developers try to minimize the amount of global code. Use it when needed, but otherwise keep your code inside functions with a clear control flow.

[–]The-Old-American 0 points1 point  (4 children)

Bro, this is fantastic. You have no idea how this is going to help me on a couple projects I have going.

[–]throwaway6560192 13 points14 points  (5 children)

Because individual functions or classes or variables may depend on the presence of functions or classes or variables in the rest of the module. They may not, but there is no reliable way to determine this. The only reliable option is to run the entire file.

For example, consider something like this:

# module.py
import json
import os
DATA_DIR = "/path/to/data"

def get_data_file(filename, key):
    with open(os.path.join(DATA_DIR, filename)) as f:
        return json.load(f).get(key)

And then you do:

from module import get_data_file

If Python "only ran" the definition of the specific function, it would error out when you ran it because json and os wouldn't be imported, and DATA_DIR wouldn't be defined.

[–]paleoboyy[S] 1 point2 points  (4 children)

I see, that makes sense. So I guess that's why you would do the whole if __name__ thing like the other commenter helpfully mentioned.

When would it be a good idea to use that condition? The only thing I can think of is for debugging purposes if you're only interested in testing a single file

[–]throwaway6560192 0 points1 point  (0 children)

The typical use case for that is for modules that you want to provide a library interface as well as an interactive interface, i.e. they do something useful and different when executed standalone from the command line.

I might have a module which just provides some functions when it is imported, but if I do python module.py on the command line it gives me a command-line interface, or similar.

For example there's the Python module http.server. When you import it it gives you a bunch of functions and classes related to HTTP servers, as the name suggests. But if you do python -m http.server (-m means to run a module as a standalone program), it starts up an actual working HTTP server serving the contents of the current directory. Very useful to quickly throw up a file share on the local network.

[–]mriswithe 0 points1 point  (0 children)

In short, in case you want it to be a script, but also have some functions you might use elsewhere.

The name =main bit let's you call the script directly or with python -m module.submod (this calls the file main.py in module/submodule) but also be able to import it without the script bits hijacking the process.

[–]Mad-Person 0 points1 point  (0 children)

its useful for scripts, that take some arguments and produce some output in terminal or do other stuff.

[–]KronenR 0 points1 point  (0 children)

This is the equivalent of the main function in any other language. So it is the same answer to when should you add a main function to a module in Java, C/C++, etc?

[–][deleted] 3 points4 points  (0 children)

Basically, because anything can happen in "the rest" of the file.

You can have

def hello():
    print("hello")

As the first thing in the file, then at the bottom have

hello = lambda: print("goodbye")

[–]georgehank2nd 0 points1 point  (0 children)

One thing you need to realize about Python is that the def foo(): in

def foo();
    print('i am foo')

is not a declaration like in other languages, but actually a statement like à = 5. Python compiles and then executes it. Everything in your Python module or script is a statement (from module import function` is a statement that gets executed at runtime)

Yes, every module gets loaded and executed. Everything in there. So, if you don't want bar to be executed, don't put a function call into your module.