What does if __name__ == "__main__" do?

Python modules, packages and environments · Freshers and experienced

Model answer

When a file runs directly, __name__ is "__main__". When it is imported, __name__ is the module name. The check lets a file work as both a script and an importable module, running the main code only when executed directly.

Learn this topic

Modules, pip and Virtual Environments in Python in the course Python for Beginners: Learn to Code with Real Projects.

Sign in to add your own answer.

More questions