1

I create two python files named "test.py" and __main__.py.

in test.py:

import __main__

in __main__.py:

print(__name__)

but when I run test.py, it outputs nothing.

then, I change __main__.py to:

def main():
    print("ok")

and test.py:

import __main__
__main__.main()

When I run test.py, it appears an error:

Traceback (most recent call last):
   File "/Users/lyz/test.py", line 2, in <module>
    __main__.main()
AttributeError: module '__main__' has no attribute 'main'

who can tell me why? Thanks a lot!

Xantium
  • 11,201
  • 10
  • 62
  • 89
lyzMaster
  • 530
  • 1
  • 6
  • 17
  • 2
    Reading [this](https://stackoverflow.com/questions/4042905/what-is-main-py_) probably help you to understand "why" – MaNKuR Mar 28 '18 at 16:28
  • 1
    I have a really bad feeling about naming a file \__main__ since this is really reserved for other purposes. Maybe someone could give a hint of the function of \__main__? – Yunkai Xiao Mar 28 '18 at 16:30
  • 1
    `__main__` is a special name and you should use it only for a module with special function. https://docs.python.org/3/library/__main__.html – VPfB Mar 28 '18 at 16:32
  • This is why `__main__.py` is a good name for a toplevel application that is not meant to be imported. Go with `__init__.py` for modules in distinct directories and you will be able to import it by naming the directory. – Bachsau Jun 16 '19 at 08:04

4 Answers4

2

As indicated by the underscores, __main__ is a magic module name. It refers to the module first executed by the Python interpreter. If you run python test.py, __main__ will be the module formed by test.py.

Instead, rename your __main__.py to main.py and use import main and main.main().

phihag
  • 278,196
  • 72
  • 453
  • 469
2

When you run python test.py the interpreter treats "test.py" as __main__ module and not test module. This is done even before reading the first line in "test.py". So when you write import __main__ it just means import test.

You can test this by writing "__main__.py" as follow:

def main():
    print("__main__ imported")

Write "test.py" as:

import __main__

def main():
    print("test imported")

__main__.main()

Now run python test.py and you will see test imported

1

You are never importing anything. Since your own file is given the name __main__ (this is true because __name__ returns '__main__'), whatever name you give your file.

You are never actually importing anything but the file you are running:

Run this throught the interpreter to see what I mean.

>>>def hello(): print('HELLO')
...
>>>import __main__
>>>__main__.hello()
HELLO
>>>
>>>
>>>__main__
<module '__main__' (built-in)>

You can quite clearly see that __main__ is an inbuilt command, and __main__.py and is never imported, only the original script.

A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

See 29.4. main — Top-level script environment

To fix the problem rename __main__.py to something that does not conflict with inbuilt commands.

Xantium
  • 11,201
  • 10
  • 62
  • 89
1

To import __main__.py you need to put it into a folder, which Python would consider a module folder then.

.
├── my_module
│   └── __main__.py
└── test.py

Then you should place test.py outside this folder and import __main__.py like this:

# test.py
from my_module import __main__
...
Simon
  • 33
  • 7