Title: What Is Python's __init__.py For? – Real Python
Open Graph Title: What Is Python's __init__.py For? – Real Python
Description: Learn to declare packages with Python's __init__.py, set package variables, simplify imports, and understand what happens if this module is missing.
Open Graph Description: Learn to declare packages with Python's __init__.py, set package variables, simplify imports, and understand what happens if this module is missing.
Mail addresses
?subject=Python article for you&body=What Is Python's __init__.py For? on Real Python
https://realpython.com/python-init-py/
Opengraph URL: https://realpython.com/python-init-py/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "What Is Python's __init__.py For?",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/What-is-__init__.py-for_Watermarked.e9b5529f58e3.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/python-init-py/",
"lastReviewed": "2026-10-30",
"author": {
"@type": "Person",
"name": "Charles de Villiers",
"image": "https://realpython.com/cdn-cgi/image/width=1500,height=1500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/real-python-logo-square.28474fda9228.png",
"url": "https://realpython.com/team/cdevilliers/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Aldren Santos",
"image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
"url": "https://realpython.com/team/asantos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Brenda Weleschuk",
"image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
"url": "https://realpython.com/team/bweleschuk/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Bartosz Zaczy\u0144ski",
"image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
"url": "https://realpython.com/team/bzaczynski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Geir Arne Hjelle",
"image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
"url": "https://realpython.com/team/gahjelle/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Martin Breuss",
"image": "https://realpython.com/cdn-cgi/image/width=456,height=456,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/martin_breuss_python_square.efb2b07faf9f.jpg",
"url": "https://realpython.com/team/mbreuss/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2025-07-09T14:00:00+00:00",
"dateModified": "2026-10-30T03:16:09.763064+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Charles de Villiers",
"image": "https://realpython.com/cdn-cgi/image/width=1500,height=1500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/real-python-logo-square.28474fda9228.png",
"url": "https://realpython.com/team/cdevilliers/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "Learn to declare packages with Python's __init__.py, set package variables, simplify imports, and understand what happens if this module is missing.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What does an empty __init__.py file do?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eAn empty \u003ccode\u003e__init__.py\u003c/code\u003e file indicates to Python that the directory is a regular package, allowing you to import modules from it.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "What happens if you omit the __init__.py file from a package directory?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eWithout \u003ccode\u003e__init__.py\u003c/code\u003e, Python recognizes the directory as a namespace package instead of a regular package. Namespace packages can span multiple directories, but their imports are usually slower.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "Can you control what gets imported from a package using __init__.py?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eYou can\u0026rsquo;t strictly enforce what\u0026rsquo;s imported, but you can guide users by explicitly defining a public API within your \u003ccode\u003e__init__.py\u003c/code\u003e file. Python conventions like leading underscores and the \u003ccode\u003e__all__\u003c/code\u003e variable help users understand your intended use.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "When does Python execute the code in __init__.py?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003ePython executes the code in \u003ccode\u003e__init__.py\u003c/code\u003e the first time you import the package or any of its modules. Python runs this code only once, caching the package for subsequent imports.\u003c/p\u003e"
}
}
]
}
}
| author | Real Python |
| theme-color | #18354c |
| color-scheme | light dark |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/What-is-__init__.py-for_Watermarked.e9b5529f58e3.jpg |
| og:image | https://files.realpython.com/media/What-is-__init__.py-for_Watermarked.e9b5529f58e3.jpg |
| twitter:creator | @realpython |
| og:type | article |
| og:site_name | Real Python |
| apple-mobile-web-app-title | Real Python |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large