Why Is Python Running My Module When I Import It, and How to Stop It

Why is Python running my module when I import it, and how do I stop it?

Because this is just how Python works - keywords such as class and def are not declarations. Instead, they are real live statements which are executed. If they were not executed your module would be empty.

The idiomatic approach is:

# stuff to run always here such as class/def
def main():
pass

if __name__ == "__main__":
# stuff only to run when not called via 'import' here
main()

It does require source control over the module being imported, however.

How to prevent modules code execution from imported module in python?

How to prevent modules code execution from module in python?

You can't, when you import a module, it runs everything that is called in the global scope.

You can change it so that it's easy to call or not:

def main():
Fu().baz()

if __name__ == '__main__':
main()

And then when you want it called you import it and call main() and it will still automatically run when you run it as the main module.

Why does importing a function from a module import the whole module?

There is no real mistery on "why" it is executed, python works this way.

The docs say:

The from form uses a slightly more complex process:

  1. find the module specified in the from clause, loading and initializing it if necessary;
  2. for each of the identifiers specified in the import clauses:

    1. check if the imported module has an attribute by that name
    2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute
    3. if the attribute is not found, ImportError is raised.
    4. otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name

And also (here the reference):

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. (They are also run if the file is executed as a script.)

This answer the question "why". Even if you load a single function from your module, the module is still initialized (makes sense to behave this way). Any "free" code hence is executed.

This means that you should not put "free" statements in a module unless they are meant to initialize it.

To prevent this behaviour, use the if __name__ == "main": statement as other answers said. Put inside this if all the code that should be executed only when the module is called directly (for example, code for testing purpose).

Python keeps looping when importing arguments from another file

The reason it runs your code is because when you import a python file, it will run the moudule. In order to stop this, you want to put your code in a main() function. Then you want to do this:

def main(): 
pass
if __name__ == '__main__':
main()

What happens is if the name of the file being ran is main, then it will run the main() function, however, when it is being imported, it wont be ran.

Trouble importing a module that itself imports another module in Python

You may want to use relative imports.

So perhaps something like :

# in module1.py

import .module2

Relative imports mean that you are not trying to import from the __main__ file but from the current file (in your case module1.py).

Edit :
Just realised I am stupid. This works for modules, not individual files.
Please try again with from . import module2



Related Topics



Leave a reply



Submit