You are probably importing the file under different names, or running it as both the __main__
file and importing it.
When Python runs your script (the file named on the command line) it gives it the name __main__
, which is a namespace stored under sys.modules
. But if you then import that same file using an import
statement, it'll be run again and the resulting namespace is stored under the module name.
Thus, python main.py
where main.py
includes an import main
statement or imports other code that then in turn imports main
again will result in all the code in main.py
to be run twice.
Another option is that you are importing the module twice under different full names; both as part of a package and as a stand-alone module. This can happen when both the directory that contains the package and the package itself are listed on your sys.path
module search path.