

{"id":9389,"date":"2018-02-27T09:58:04","date_gmt":"2018-02-27T04:28:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=9389"},"modified":"2026-04-28T14:47:50","modified_gmt":"2026-04-28T09:17:50","slug":"python-packages","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-packages\/","title":{"rendered":"Python Packages &#8211; How to Create Your Own Package"},"content":{"rendered":"<p>Are you tired of searching through those messy files or copying and pasting the same code over and over again?<\/p>\n<p>Imagine that Python Package is a professional toolkit for your code. Instead of having a bunch of shuffled scripts, you can create a neat, organized folder that you can use anywhere. Whether you want to stay organized or share your work with the world. Creating a package is the best way to code like a pro.<\/p>\n<p>In today\u2019s article, we will discuss Python Packages. Moreover, we will learn the structure of Python Packages and how to import modules from packages in Python.<\/p>\n<p>So, let&#8217;s start the Python Packages Tutorial.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-9450 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01.jpg\" alt=\"Python Packages\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>Python Packages Tutorial &#8211; How to Create Your Own Package in Python<\/p>\n<h3>What are Python Packages?<\/h3>\n<p>In our computer systems, we store our files in organized hierarchies. We don\u2019t store them all in one location. Likewise, when our programs grow, we divide them into packages.<\/p>\n<p>In real-life projects, programs are much larger than what we deal with in our journey of learning Python. A package lets us hold similar modules in one place.<\/p>\n<p>Like a directory may contain subdirectories and files, a package may contain sub-packages and modules. We have been using modules a lot in the previous lessons.<\/p>\n<p>Remember math, os, and collections? Those were all modules that ship with Python officially. We will discuss the difference between a module and a package in our next lesson.<\/p>\n<p>But for now, let\u2019s dive into the world of Python packages.<\/p>\n<h3>Structure of Python Packages<\/h3>\n<p>As we discussed, a package may hold other Python packages and modules. But what distinguishes a package from a regular directory? Well, a Python package must have an __init__.py file in the directory.<\/p>\n<p>You may leave it empty, or you may store initialization code in it. But if your directory does not have an __init__.py file, it isn\u2019t a package; it is just a directory with a bunch of Python scripts. Leaving __init__.py empty is indeed good practice.<\/p>\n<p>Take a look at the following structure for a game:<\/p>\n<div id=\"attachment_9427\" style=\"width: 1018px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9427\" class=\"wp-image-9427 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure.png\" alt=\"Python Packages Module Structure\" width=\"1008\" height=\"724\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure.png 1008w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure-150x108.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure-300x215.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Package-Module-Structure-768x552.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><\/a><p id=\"caption-attachment-9427\" class=\"wp-caption-text\">Python Packages Module Structure<\/p><\/div>\n<p>Here, the root package is Game. It has sub packages Sound, Image, and Level, and file __init__.py. Sound further has modules load, play, and pause, apart from file __init__.py.<\/p>\n<p>Image has modules open, change, and close, apart from __init__.py. Finally, Level has modules start, load, and over, apart from __init__.py.<\/p>\n<h3>How to Import Modules from Packages in Python?<\/h3>\n<p>A Python package may contain several modules. To import one of these into your program, you must use the dot operator(.)<\/p>\n<p>In the above example, if you want to import the load module from the subpackage sound, we type the following at the top of our Python file:<\/p>\n<p>import Game.Sound.load<\/p>\n<p>Note that we don\u2019t type the extension, because that isn\u2019t what we refer to the module as. The subpackage Level has a module named load, too, but there is no clash here.<\/p>\n<p>This is because we refer to the module by its fully qualified name.<\/p>\n<p>To escape having to type so much every time we needed to use the module, we could also import it under an alias:<\/p>\n<p>import Game.Sound.load as loadgame<\/p>\n<p>(If you\u2019re working the interpreter, you may also do the following:<br \/>\nloadgame=Game.Sound.load<\/p>\n<p>This works equally fine.)<\/p>\n<p>Alternatively, you could do:<\/p>\n<p>from Game.Sound import load<\/p>\n<p>Now, if the Sound subpackage has a function volume_up(), we call it this way:<br \/>\nloadgame.volume_up(7)<\/p>\n<p>If we imported this way:<\/p>\n<p>from Game.Sound.load import volume_up() as volup<\/p>\n<p>We could call the function simply, without needing to use a full qualifier:<br \/>\nvolup(7)<\/p>\n<p>But this isn\u2019t recommended, as this may cause names in a namespace to clash.<\/p>\n<h3>Further Notes<\/h3>\n<p>When you import a package, only the modules directly under it are imported. An import does not import the sub-packages.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import one\r\n&gt;&gt;&gt; one.two<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#488&gt;&#8221;, line 1, in &lt;module&gt;one.two.evenoddAttributeError: module &#8216;one&#8217; has no attribute &#8216;two&#8217;<\/p>\n<\/div>\n<p>Also note that if you want to check where your Python packages are being created, your path will look something like this:<\/p>\n<p>C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages<\/p>\n<h3>How to Create Your Own Python Package?<\/h3>\n<p>Now, on to the most interesting part. Like we said, Python packages are nothing but a dictionary with sub-packages and modules, and an __init__.py file.<\/p>\n<p>In our example, this is the hierarchy we create:<\/p>\n<div id=\"attachment_9430\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9430\" class=\"wp-image-9430 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package.png\" alt=\"Python Packages\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package.png 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package-300x157.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package-768x402.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Creating-Your-Own-Package-1024x536.png 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-9430\" class=\"wp-caption-text\">How to Create Your Own Python Package<\/p><\/div>\n<p>This is what we have in evenodd.py:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def check():\r\n          a=int(input('Enter a number'))\r\n          if a%2==0: print(\"Even\")\r\n          else: print(\"Odd\")<\/pre>\n<p>Also, we keep each __init__.py empty.<\/p>\n<p>Now, we import and use it this way:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from one.two.evenodd import check as check\r\n&gt;&gt;&gt; check()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Enter a number7<br \/>\nOdd<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; check()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Enter a number0<br \/>\nEven<\/div>\n<p>So, this was all about Python Packages. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Packages<\/h3>\n<ol>\n<li>What are the Python Packages?<\/li>\n<li>How many packages are there in Python?<\/li>\n<li>What are Python Dist Packages?<\/li>\n<li>How to create a Python package?<\/li>\n<li>Why do we need packages in Python?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p>In this Python Packages tutorial, we discussed packages and how to create them. Apart from that, the Python Package Index(PyPI) provides us with a lot of Python packages to help us with our projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you tired of searching through those messy files or copying and pasting the same code over and over again? Imagine that Python Package is a professional toolkit for your code. Instead of having&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":36340,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[6643,16504,10333,10753,10754],"class_list":["post-9389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-importing-modules","tag-packages-in-python","tag-python","tag-python-package-module-structure","tag-python-packages"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Packages - How to Create Your Own Package - DataFlair<\/title>\n<meta name=\"description\" content=\"In this Python Packages tutorial, we will learn what packages are, their structure, and how to import modules from packages in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-packages\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Packages - How to Create Your Own Package - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this Python Packages tutorial, we will learn what packages are, their structure, and how to import modules from packages in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-packages\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-27T04:28:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T09:17:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Packages - How to Create Your Own Package - DataFlair","description":"In this Python Packages tutorial, we will learn what packages are, their structure, and how to import modules from packages in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-packages\/","og_locale":"en_US","og_type":"article","og_title":"Python Packages - How to Create Your Own Package - DataFlair","og_description":"In this Python Packages tutorial, we will learn what packages are, their structure, and how to import modules from packages in Python.","og_url":"https:\/\/data-flair.training\/blogs\/python-packages\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-27T04:28:04+00:00","article_modified_time":"2026-04-28T09:17:50+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Packages &#8211; How to Create Your Own Package","datePublished":"2018-02-27T04:28:04+00:00","dateModified":"2026-04-28T09:17:50+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/"},"wordCount":899,"commentCount":11,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg","keywords":["Importing Modules","Packages in Python","Python","Python PAckage Module Structure","Python Packages"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-packages\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-packages\/","url":"https:\/\/data-flair.training\/blogs\/python-packages\/","name":"Python Packages - How to Create Your Own Package - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg","datePublished":"2018-02-27T04:28:04+00:00","dateModified":"2026-04-28T09:17:50+00:00","description":"In this Python Packages tutorial, we will learn what packages are, their structure, and how to import modules from packages in Python.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-packages\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Packages-01-1.jpg","width":1200,"height":628,"caption":"Python Packages Tutorial - How to Create Your Own Package"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-packages\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Packages &#8211; How to Create Your Own Package"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/9389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=9389"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/9389\/revisions"}],"predecessor-version":[{"id":148014,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/9389\/revisions\/148014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/36340"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=9389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=9389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=9389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}