Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

92%
+21 −0
Q&A What is the purpose of `if __name__ == '__main__'`?

It makes difference if the script is being imported. Let's suppose I have a file my_file.py: # my_file.py def some_function(): print('do some stuff') print('calling function:') some_fun...

posted 3y ago by hkotsubo‭  ·  edited 3y ago by hkotsubo‭

Answer
#3: Post edited by user avatar hkotsubo‭ · 2021-10-21T17:37:27Z (about 3 years ago)
  • It makes difference if the script is being imported.
  • Let's suppose I have a file `my_file.py`:
  • ```python
  • # my_file.py
  • def some_function():
  • print('do some stuff')
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (such as `python my_file.py`), the output is:
  • ```none
  • calling function:
  • do some stuff
  • ```
  • Now suppose that I have another file (`another.py`), that imports `my_file.py` and uses `some_function`:
  • ```python
  • # another.py
  • import my_file
  • print('calling function from my_file')
  • my_file.some_function()
  • ```
  • By executing `python another.py`, the output will be:
  • ```none
  • calling function:
  • do some stuff
  • calling function from my_file
  • do some stuff
  • ```
  • That's because the `import` executes the whole contents of `my_file.py` (the first two lines in the output), and then `another.py` calls the function again.
  • To avoid that, we can change `my_file.py` to this:
  • ```python
  • # my_file.py modified
  • def some_function():
  • print('do some stuff')
  • if __name__ == "__main__":
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (`python my_file.py`), it still executes the two lines inside the `if` (`print` and `some_function()`).
  • But if I execute `python another.py`, the output will be:
  • ```none
  • calling function from my_file
  • do some stuff
  • ```
  • Because when `my_file.py` is imported, the code inside that `if` is not executed.
  • ---
  • That happens because, when a Python source file is executed, some special variables are set, and `__name__` is one of them.
  • When the file is being executed directly (such as `python my_file.py`), the `__name__` variable is set to `__main__`. But when `my_file.py` is imported, its `__name__` variable is set to `my_file`.
  • Let's modify the files to see how this works.
  • `my_file.py`:
  • ```python
  • print('my_file=', __name__)
  • ```
  • `another.py`:
  • ```python
  • import my_file
  • print('another=', __name__)
  • ```
  • If I execute `python my_file.py`, the output is:
  • ```none
  • my_file= __main__
  • ```
  • But if I execute `python another.py`, the output is:
  • ```none
  • my_file= my_file
  • another= __main__
  • ```
  • Note that, for the file being executed, the `__name__` variable contains the value `__main__`, and for the imported files, the `__name__` variable is the file's name without the `.py` extension.
  • The code above also shows that each module has its own `__name__`. This means that `another.py` could also have the same `if` clause inside it.
  • ---
  • This is useful when a module contains lots of functions/classes that you want to import and use, but it also contains a "main" part that acts as a script by itself (this could be the main program, or unit tests, or a "demo" for the functions, or whatever). This "main" part can be put inside that `if` clause, so you don't run it when the module is imported, as in this case you're interested only in reusing the functions/classes.
  • It makes difference if the script is being imported.
  • Let's suppose I have a file `my_file.py`:
  • ```python
  • # my_file.py
  • def some_function():
  • print('do some stuff')
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (such as `python my_file.py`), the output is:
  • ```none
  • calling function:
  • do some stuff
  • ```
  • Now suppose that I have another file (`another.py`), that imports `my_file.py` and uses `some_function`:
  • ```python
  • # another.py
  • import my_file
  • print('calling function from my_file')
  • my_file.some_function()
  • ```
  • By executing `python another.py`, the output will be:
  • ```none
  • calling function:
  • do some stuff
  • calling function from my_file
  • do some stuff
  • ```
  • That's because the `import` executes the whole contents of `my_file.py` (producing the first two lines in the output), and then `another.py` calls the function again.
  • To avoid that, we can change `my_file.py` to this:
  • ```python
  • # my_file.py modified
  • def some_function():
  • print('do some stuff')
  • if __name__ == "__main__":
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (`python my_file.py`), it still executes the two lines inside the `if` (`print` and `some_function()`).
  • But if I execute `python another.py`, the output will be:
  • ```none
  • calling function from my_file
  • do some stuff
  • ```
  • Because when `my_file.py` is imported, the code inside that `if` is not executed.
  • ---
  • That happens because, when a Python source file is executed, some special variables are set, and `__name__` is one of them.
  • When the file is being executed directly (such as `python my_file.py`), the `__name__` variable is set to `__main__`. But when `my_file.py` is imported, its `__name__` variable is set to `my_file`.
  • Let's modify the files to see how this works.
  • `my_file.py`:
  • ```python
  • print('my_file=', __name__)
  • ```
  • `another.py`:
  • ```python
  • import my_file
  • print('another=', __name__)
  • ```
  • If I execute `python my_file.py`, the output is:
  • ```none
  • my_file= __main__
  • ```
  • But if I execute `python another.py`, the output is:
  • ```none
  • my_file= my_file
  • another= __main__
  • ```
  • Note that, for the file being executed, the `__name__` variable contains the value `__main__`, and for the imported files, the `__name__` variable is the file's name without the `.py` extension.
  • The code above also shows that each module has its own `__name__`. This means that `another.py` could also have the same `if` clause inside it, to check if it's being imported, regardless of other module's `__name__`'s.
  • ---
  • This is useful when a module contains lots of functions/classes that you want to import and use, but it also contains a "main" part that acts as a script by itself (this could be the main program, or unit tests, or a "demo" for the functions, or whatever). This "main" part can be put inside that `if` clause, so you don't run it when the module is imported, as in this case you're interested only in reusing the functions/classes.
#2: Post edited by user avatar hkotsubo‭ · 2021-10-21T17:29:32Z (about 3 years ago)
  • It makes difference if the script is being imported.
  • Let's suppose I have a file `my_file.py`:
  • ```python
  • # my_file.py
  • def some_function():
  • print('do some stuff')
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (such as `python my_file.py`), the output is:
  • ```none
  • calling function:
  • do some stuff
  • ```
  • Now suppose that I have another file (`another.py`), that imports `my_file.py` and uses `some_function`:
  • ```python
  • # another.py
  • import my_file
  • print('calling function from my_file')
  • my_file.some_function()
  • ```
  • By executing `python another.py`, the output will be:
  • ```none
  • calling function:
  • do some stuff
  • calling function from my_file
  • do some stuff
  • ```
  • That's because the `import` executes the whole contents of `my_file.py` (the first two lines in the output), and then `another.py` calls the function again.
  • To avoid that, we can change `my_file.py` to this:
  • ```python
  • # my_file.py modified
  • def some_function():
  • print('do some stuff')
  • if __name__ == "__main__":
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (`python my_file.py`), it still executes the two lines inside the `if` (`print` and `some_function()`).
  • But if I execute `python another.py`, the output will be:
  • ```none
  • calling function from my_file
  • do some stuff
  • ```
  • Because when `my_file.py` is imported, the code inside that `if` is not executed.
  • ---
  • That happens because, when a Python source file is executed, some special variables are set, and `__name__` is one of them.
  • When the file is being executed directly (such as `python my_file.py`), the `__name__` variable is set to `__main__`. But when `my_file.py` is imported, its `__name__` variable is set to `my_file`.
  • Let's modify the files to see how this works.
  • `my_file.py`:
  • ```python
  • print('my_file=', __name__)
  • ```
  • `another.py`:
  • ```python
  • import my_file
  • print('another=', __name__)
  • ```
  • If I execute `python my_file.py`, the output is:
  • ```none
  • my_file= __main__
  • ```
  • But if I execute `python another.py`, the output is:
  • ```none
  • my_file= my_file
  • another= __main__
  • ```
  • Note that, for the file being executed, the `__name__` variable contains the value `__main__`, and for the imported files, the `__name__` variable is the file's name without the `.py` extension.
  • ---
  • This is useful when a module contains lots of functions/classes that you want to import and use, but it also contains a "main" part that acts as a script by itself (or unit tests, or a "demo" for the functions, or whatever). This "main" part can be put inside that `if` clause, so you don't run it when the module is imported, as in this case you're interested only in reusing the functions/classes.
  • It makes difference if the script is being imported.
  • Let's suppose I have a file `my_file.py`:
  • ```python
  • # my_file.py
  • def some_function():
  • print('do some stuff')
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (such as `python my_file.py`), the output is:
  • ```none
  • calling function:
  • do some stuff
  • ```
  • Now suppose that I have another file (`another.py`), that imports `my_file.py` and uses `some_function`:
  • ```python
  • # another.py
  • import my_file
  • print('calling function from my_file')
  • my_file.some_function()
  • ```
  • By executing `python another.py`, the output will be:
  • ```none
  • calling function:
  • do some stuff
  • calling function from my_file
  • do some stuff
  • ```
  • That's because the `import` executes the whole contents of `my_file.py` (the first two lines in the output), and then `another.py` calls the function again.
  • To avoid that, we can change `my_file.py` to this:
  • ```python
  • # my_file.py modified
  • def some_function():
  • print('do some stuff')
  • if __name__ == "__main__":
  • print('calling function:')
  • some_function()
  • ```
  • If I execute it directly (`python my_file.py`), it still executes the two lines inside the `if` (`print` and `some_function()`).
  • But if I execute `python another.py`, the output will be:
  • ```none
  • calling function from my_file
  • do some stuff
  • ```
  • Because when `my_file.py` is imported, the code inside that `if` is not executed.
  • ---
  • That happens because, when a Python source file is executed, some special variables are set, and `__name__` is one of them.
  • When the file is being executed directly (such as `python my_file.py`), the `__name__` variable is set to `__main__`. But when `my_file.py` is imported, its `__name__` variable is set to `my_file`.
  • Let's modify the files to see how this works.
  • `my_file.py`:
  • ```python
  • print('my_file=', __name__)
  • ```
  • `another.py`:
  • ```python
  • import my_file
  • print('another=', __name__)
  • ```
  • If I execute `python my_file.py`, the output is:
  • ```none
  • my_file= __main__
  • ```
  • But if I execute `python another.py`, the output is:
  • ```none
  • my_file= my_file
  • another= __main__
  • ```
  • Note that, for the file being executed, the `__name__` variable contains the value `__main__`, and for the imported files, the `__name__` variable is the file's name without the `.py` extension.
  • The code above also shows that each module has its own `__name__`. This means that `another.py` could also have the same `if` clause inside it.
  • ---
  • This is useful when a module contains lots of functions/classes that you want to import and use, but it also contains a "main" part that acts as a script by itself (this could be the main program, or unit tests, or a "demo" for the functions, or whatever). This "main" part can be put inside that `if` clause, so you don't run it when the module is imported, as in this case you're interested only in reusing the functions/classes.
#1: Initial revision by user avatar hkotsubo‭ · 2021-10-21T17:21:29Z (about 3 years ago)
It makes difference if the script is being imported.

Let's suppose I have a file `my_file.py`:

```python
# my_file.py
def some_function():
    print('do some stuff')

print('calling function:')
some_function()
```

If I execute it directly (such as `python my_file.py`), the output is:

```none
calling function:
do some stuff
```

Now suppose that I have another file (`another.py`), that imports `my_file.py` and uses `some_function`:

```python
# another.py
import my_file

print('calling function from my_file')
my_file.some_function()
```

By executing `python another.py`, the output will be:

```none
calling function:
do some stuff
calling function from my_file
do some stuff
```

That's because the `import` executes the whole contents of `my_file.py` (the first two lines in the output), and then `another.py` calls the function again.

To avoid that, we can change `my_file.py` to this:

```python
# my_file.py modified
def some_function():
    print('do some stuff')

if __name__ == "__main__":
    print('calling function:')
    some_function()
```

If I execute it directly (`python my_file.py`), it still executes the two lines inside the `if` (`print` and `some_function()`).

But if I execute `python another.py`, the output will be:

```none
calling function from my_file
do some stuff
```

Because when `my_file.py` is imported, the code inside that `if` is not executed.

---
That happens because, when a Python source file is executed, some special variables are set, and `__name__` is one of them.

When the file is being executed directly (such as `python my_file.py`), the `__name__` variable is set to `__main__`. But when `my_file.py` is imported, its `__name__` variable is set to `my_file`.

Let's modify the files to see how this works.

`my_file.py`:

```python
print('my_file=', __name__)
```

`another.py`:

```python
import my_file

print('another=', __name__)
```

If I execute `python my_file.py`, the output is:

```none
my_file= __main__
```

But if I execute `python another.py`, the output is:

```none
my_file= my_file
another= __main__
```

Note that, for the file being executed, the `__name__` variable contains the value `__main__`, and for the imported files, the `__name__` variable is the file's name without the `.py` extension.

---
This is useful when a module contains lots of functions/classes that you want to import and use, but it also contains a "main" part that acts as a script by itself (or unit tests, or a "demo" for the functions, or whatever). This "main" part can be put inside that `if` clause, so you don't run it when the module is imported, as in this case you're interested only in reusing the functions/classes.