1

First of all I have to say, that I'm not new in Python. I know the use of if __name__ == '__main__': well enough. I just try to understand how the Python interpreter handles this. I wrote two scripts to test what's possible in Python. Note that that's actually really bad Python - I wouldn't make use of such things. I only want to learn about the possibilities.

main.py:

import bad.py


def main():

    print "Hello, World!"


if __name__ == '__main__':

    main()

bad.py:

__name__ = '__main__' # I expected an Error caused by this line


def main():

    exit() # I know that this function shouldn't be used


if __name__ == '__main__':

    main()

There's no output by running main.py and I didn't really expect an output.

what I want to know:

  • does __name__ = '__main__' in bad.py automatically change the variable __name__ in main.py? Or can two different '__main__'-modules exist?
  • is it possible to substitute an import statement just by manipulating __name__? If it is, how would you do that?
  • when I change exit() to print "Hello",, The output is "Hello", then an Error occurs:

    Traceback (most recent call last):
      File "main.py", line 1, in <module>
        import bad.py
    ImportError: No module named py
    

    could someone explain this behaviour?

  • Are there any differences between python versions / operating systems? I tried this example in Python 2.7.6 on Linux2

EDIT:

Of course import bad.py doesn't work. import-statements don't want any file-endings. I did this mistake because I also program in C. I changed the line to import bad. The output looks like this now:

Hello Hello, World
Aemyl
  • 1,501
  • 1
  • 19
  • 34
  • Take a look at this question, http://stackoverflow.com/questions/15883526/how-is-the-name-variable-in-a-python-module-defined . The answer is what you want. The `__name__` changes depending on how you might import a file in python. – Preston Hager Dec 13 '16 at 12:42
  • @PrestonHager excuse me that I couldn't write _every_ point in the question's title - the answer of your linked question doesn't answer every point of my question. If these points are also duplicates, please leave a link to the other questions as well – Aemyl Dec 13 '16 at 12:50
  • Ok, the answer for if the `__name__` variable can be used for an import is no it can't. Python must call the `__import__()` function. The other question on system exit is interesting. There are no differences in these functions between Python 2 and 3, though I don't know for operating systems. – Preston Hager Dec 13 '16 at 12:56
  • Have you searched the python documentation for references to `__name__`? – Bryan Oakley Dec 13 '16 at 13:02
  • Here's a pretty decent explanation: https://docs.python.org/3/library/__main__.html – Will Dec 13 '16 at 13:17

1 Answers1

0

change your import statement to import bad

The if __name__ == '__main__': intention is to run this code if this program is run directly, as a main program. As you see you can overwrite the name value but the same effect would occur if you remove the if check in bad.

  • __name__ in bad.py is independent from __name__ in main.py. You can see that when you again call main.py from another program.
  • the import function is completely different from the __name__ attribute. So no.
  • you got the error because main continues with the next import after bad was finished. And it cannot import py.

Add some more print statements to follow the logic. You can also print __name__ itself to see those values.

Gerrit Verhaar
  • 446
  • 4
  • 12
  • 1.) my import statement is already `import bad.py`. I don't know if you actually meant something else. – Aemyl Dec 13 '16 at 13:33
  • 2.) if I run `main.py`from another program again, it is completely new interpreted. Of course it is independent in this case, so this check doesn't make sense – Aemyl Dec 13 '16 at 13:34
  • 3.) As far as I know, the `import` keyword has influence to the value of `__name__` (in the imported script) – Aemyl Dec 13 '16 at 13:36
  • my fault. I meant `import bad`. remove the .py – Gerrit Verhaar Dec 13 '16 at 13:36
  • Oh, of course ;) probably I programmed too much in C. I fixed it, now the output is `Hello Hello, World!` (without linebreak) as I expected – Aemyl Dec 13 '16 at 13:39
  • this also gives a good description: http://stackoverflow.com/questions/419163/what-does-if-name-main-do – Gerrit Verhaar Dec 13 '16 at 13:45
  • I read this question already and wouldn't have asked if it answered every of my question points – Aemyl Dec 13 '16 at 13:48