Hongmu Notes
Home Language Notes What does `if __name__ == '__main__' ` mean in Python?
Language Notes python

What does `if __name__ == '__main__' ` mean in Python?

What does `if __name__ == '__main__' ` mean in Python?

if __name__ == '__main__':This is a commonly used conditional statement in Python that allows a Python file to be executed as an independent script or imported as a module by other Python scripts. Specifically, when a Python file is executed as a script,if __name__ == '__main__':The following code block will be executed; this occurs when the file is imported by another Python script.if __name__ == '__main__':The following code block will not be executed.

For example, suppose we have two Python files:script.pymodule.pyscript.pyIt is an executable script file, whilemodule.pyIt is a Python file that can be imported as a module. Thus, we canscript.pyWrite it like this:

import module

if __name__ == '__main__':
    # 以下是脚本文件的执行代码
    # ...

In this example, wescript.pyIntroduced into ChinamoduleModule – Useif __name__ == '__main__':Determine whether to execute the code block that contains a script file. Whenscript.pyWhen executed as a script,if __name__ == '__main__':The following code block will be executed; and whenmodule.pyWhen imported by other Python scripts,if __name__ == '__main__':The following code block will not be executed.

Useif __name__ == '__main__':It allows us to write executable scripts and Python files that can be imported as modules more conveniently, thereby enhancing code reusability and maintainability.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Import " requests" could not be resolved from source

Import " requests" could not be resolved from source Language Notes python

This error occurred when using Pylance, a Python language server. Among them, " Import " requests" could not be resolved from source" This error message indicates that the requests module is referenced in your code, but Pylance cannot find the location of the module from the source. The possible reason is that you don't have an …
👁 1771
Reinstall Python and requests libraries

Reinstall Python and requests libraries Language Notes python

If you meet " Import " requests" could not be resolved from source" You can try reinstalling Python and requests libraries. First, you need to uninstall the current Python and requests libraries. On Windows systems, you can use the command prompt …
👁 439
WARNING: Skipping requests as it is not installed.

WARNING: Skipping requests as it is not installed. Language Notes python

This error message indicates that your Python environment lacks the requests library, and this library is called in your code, so Python cannot execute your code normally. The solution to this problem is to install the requests library. You can use the pip command on the command line to install: pip install requests If you are using Python3, you can use the pip3 command …
👁 2100
Python reported an error. What is the reason? AttributeError: partially initialized module ' openai'  has no attribute ' Completion' (most likely due to a circular import)

Python reported an error. What is the reason? AttributeError: partially initialized module ' openai' has no attribute ' Completion' (most likely due to a circular import) Language Notes python

This error prompt indicates that there is a problem when importing the openai module and the specific Completion attribute, which is probably caused by circular import. The reason for this error may be that two or more modules in your code are imported from each other, forming a circular dependency. For example, module A imports module B, and module B imports module A.. Cyclic import will cause the module not to initialize correctly when loading, thus …
👁 656
How does python package and run on Windows?

How does python package and run on Windows? Language Notes python

The most common way to package and run Python programs on Windows is to use PyInstaller. PyInstaller is a free and cross-platform Python application packaging tool, which can package Python code and its dependent libraries into an independent executable file, making it possible to run Python programs on systems without Python interpreters installed. The following is to make …
👁 300
What programming software is good for python development?

What programming software is good for python development? Language Notes python

There are many programming softwares developed by Python. The following are some commonly used Python integrated development environments (IDE) and editors: PyCharmPyCharm is an integrated development environment developed by JetBrains, which is specially designed for Python developers. PyCharm is powerful and provides a series of advanced functions such as code completion, debugging, version control, code refactoring, etc., which can help developers improve their work …
👁 175

Recommended reading

Responsive electronic component PCB website template – 1015

Responsive electronic component PCB website template – 1015 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the electronic components and circuit board industries. Its modern, precision-oriented design effectively showcases electronic components, circuit board products, and technical specifications. It enables electronics manufacturing enterprises to showcase their products online and attract customers within the electronics sector. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 62
Responsive scrolling photo album website template 0194

Responsive scrolling photo album website template 0194 Practical Collection Yiyou template

An EYO CMS responsive website template designed for the scrolling gallery and photo album industry. Its creative visual design and full-screen scrolling effect vividly showcase photographic works and brand stories. This template helps photography agencies present their premium brand image online and attract customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 52
(Adaptive mobile version) Responsive desktop and mobile accessory website – PBootCMS template; Download source code for an HTML5 e-commerce digital product accessories website – 0570

(Adaptive mobile version) Responsive desktop and mobile accessory website – PBootCMS template; Download source code for an HTML5 e-commerce digital product accessories website – 0570 Practical Collection pbootcms Template

A responsive computer and mobile accessory website template compatible with both PC and WAP devices, featuring a modern, tech-inspired design ideal for showcasing mobile phone cases, computer peripherals, and other accessory products. It helps digital accessory brands attract younger consumers online. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 46
What impact might timestamp-based防盗链机制 have on CDN caching performance?

What impact might timestamp-based防盗链机制 have on CDN caching performance? Summary of pitfalls

Timestamp-based防盗链机制 may impact CDN caching performance in the following ways: Reduced cache hit rate: Since each request generates a new URL, requests for the same resource URL at different times are effectively distinct; this can prevent the CDN cache from finding a cached entry, thereby reducing the cache hit rate. Increased cache capacity: Because each timestamp corresponds to a unique URL, the timestamp-based防盗link mechanism increases the number of URLs in the CDN cache, from...
👁 170
Semiconductor electronic components website template 1016

Semiconductor electronic components website template 1016 Practical Collection Yiyou template

An eyouCMS website template designed specifically for semiconductor and electronic component manufacturers. Its modern, high-tech design is ideal for showcasing semiconductor products, electronic components, and technical solutions. This template helps semiconductor companies present their products online and attract customers from the electronics industry. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YouYouCMS | YouYouCMS...
👁 48
Responsive, marketing-oriented automotive parts website template – 0195

Responsive, marketing-oriented automotive parts website template – 0195 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the automotive parts marketing sector, featuring a professional marketing design style that effectively showcases automotive parts products, technical specifications, brand advantages, and after-sales support. It enables automotive parts enterprises to showcase their products online and attract targeted customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 33