{"id":390,"date":"2019-07-10T09:25:12","date_gmt":"2019-07-10T09:25:12","guid":{"rendered":"http:\/\/askpython.com\/?p=390"},"modified":"2026-09-29T16:18:24","modified_gmt":"2026-09-29T16:18:24","slug":"python-packages","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-packages","title":{"rendered":"Python Packages &#8211; Creating and Accessing"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Python packages help us to manage modules and python scripts. They are normal directories with a init script &#8211; __init__.py.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to Create a Python Package?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can create a package by following below steps.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Create the package directory <\/strong>&#8211; we can use terminal or Python IDE for this.<\/li><li><strong>Create __init__.py file<\/strong> &#8211; this is required to convert a normal directory into python package. This file is used to initialize the package and list all the modules. In the simplest form, this file can be empty.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What can we keep in a Python Package?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The initialization file<\/li><li>Python modules<\/li><li>Python scripts<\/li><li>Any other type of files<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So in general, a package just like is a directory in our computer systems. The only difference is the mandatory inclusion of the __init__.py file.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Can we Create sub-packages in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, we can create a package inside another package. We have to follow the packaging rules to create a sub-package too.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Package Examples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look into some examples of creating and using packages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creating a Package<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ mkdir utilities\n$ touch utilities\/__init__.py\n$ mkdir utilities\/strings\n$ mkdir utilities\/strings\/__init__.py\n$ tree\n.\n\u2514\u2500\u2500 utilities\n    \u251c\u2500\u2500 __init__.py\n    \u2514\u2500\u2500 strings\n        \u2514\u2500\u2500 __init__.py\n\n3 directories, 1 file\n$ \n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"486\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-packages.png\" alt=\"Python Packages\" class=\"wp-image-391\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-packages.png 1006w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-packages-300x145.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-packages-768x371.png 768w\" sizes=\"auto, (max-width: 1006px) 100vw, 1006px\" \/><figcaption>Python Packages Structure<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Adding Modules to the Package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we have two python modules &#8211; <strong>math.py<\/strong> and <strong>str_utils.py<\/strong>. They have few <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\" rel=\"noreferrer noopener\" label=\"functions (opens in a new tab)\">functions<\/a> that will be used in our program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>math.py:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef add(x, y):\n    return x + y\n\n\ndef multiply(x, y):\n    return x * y\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>str_utils.py:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef to_uppercase(s):\n    s = str(s)\n    return s.upper()\n\n\ndef reverse(s):\n    s = str(s)\n    return s&#x5B;::-1]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We want to add these modules to our packages. Just copy these files to the packages directory where you want to keep these modules.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n$ ls\nmath.py      str_utils.py utilities\n$ mv math.py utilities \n$ mv str_utils.py utilities\/strings \n$ tree\n.\n\u2514\u2500\u2500 utilities\n    \u251c\u2500\u2500 __init__.py\n    \u251c\u2500\u2500 math.py\n    \u2514\u2500\u2500 strings\n        \u251c\u2500\u2500 __init__.py\n        \u2514\u2500\u2500 str_utils.py\n\n3 directories, 3 files\n$ \n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"550\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/adding-modules-to-python-package.png\" alt=\"Adding Modules To Python Package\" class=\"wp-image-392\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/adding-modules-to-python-package.png 984w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/adding-modules-to-python-package-300x168.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/adding-modules-to-python-package-768x429.png 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><figcaption>Adding Modules To Python Package<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Importing Modules from a Package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax to import a python module inside a package is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport package.sub_package1.sub_package2.module\nimport package.sub_package1.sub_package2.module as module\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Python uses <code>sys.path<\/code> variable to search for packages and modules. The current directory is part of the <code>sys.path<\/code> variable. So we will keep our python script in the <strong>python-packages<\/strong> directory. Otherwise, we will have to add the package location in the <code>sys.path<\/code> variable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n$ cat my_script.py \nimport sys\n\nprint(sys.path)\n$ python3.7 my_script.py \n&#x5B;&#039;\/Users\/pankaj\/Desktop\/python-packages&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python37.zip&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7\/lib-dynload&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7\/site-packages&#039;]\n$ \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here is the my_script.py code to access the modules from the packages and calling their functions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport utilities.math as math\nimport utilities.strings.str_utils as str_utils\n\nprint(math.add(10, 20))\nprint(str_utils.reverse(&quot;ABC&quot;))\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"370\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/importing-package-module-in-python.png\" alt=\"Importing Package Module In Python\" class=\"wp-image-394\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/importing-package-module-in-python.png 868w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/importing-package-module-in-python-300x128.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/importing-package-module-in-python-768x327.png 768w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><figcaption>Importing Package Module In Python<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">We can also import a module using below syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom package.sub_package1.sub_package2 import module\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here is the updated example to access &#8220;math&#8221; and &#8220;str_utils&#8221; module in our program.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom utilities import math\nfrom utilities.strings import str_utils\n\nprint(math.add(10, 20))\nprint(str_utils.reverse(&quot;ABC&quot;))\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"380\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-from-package-import-module.png\" alt=\"Python From Package Import Module\" class=\"wp-image-395\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-from-package-import-module.png 760w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-from-package-import-module-300x150.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><figcaption>Python &#8211; from Package Import Module<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Import * from a package<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We can import every module from a package using the following syntax.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom package.sub_package1.sub_package2 import *\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In this case, Python searches sub_package2 for packages, modules, and functions. This can produce side effects by importing something that you don&#8217;t want to. Also, it&#8217;s a very time taking process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can define a list of modules to be imported by creating <strong>__all__<\/strong> variable in the __init__.py file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>utilities\/__init__.py:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;utilities package initialized&#039;)\n\n__all__ = &#x5B;&#039;math&#039;]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>utilities\/strings\/__init__.py:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&#039;utilities.strings package initialized&#039;)\n\n__all__ = &#x5B;&#039;str_utils&#039;]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The updated my_script.py code is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom utilities import *\nfrom utilities.strings import *\n\nprint(math.add(10, 20))\nprint(str_utils.reverse(&quot;ABC&quot;))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n$ python3.7 my_script.py \nutilities package initialized\nutilities.strings package initialized\n30\nCBA\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"972\" height=\"792\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-package-init-file-all-modules-list.png\" alt=\"Python Package Init File All Modules List\" class=\"wp-image-396\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-package-init-file-all-modules-list.png 972w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-package-init-file-all-modules-list-300x244.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-package-init-file-all-modules-list-768x626.png 768w\" sizes=\"auto, (max-width: 972px) 100vw, 972px\" \/><figcaption>Python Package Init File All Modules List Example<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that the python code in the __init__.py gets executed first when the packages are initialized and imported.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to add a Package to the System Path<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s not feasible to always depend on the directory hierarchy to import package modules. We can add our custom package to the sys.path variable and then import them in any script.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport sys\n\nsys.path.append(&quot;\/Users\/pankaj\/Desktop\/python-packages&quot;)\n\nprint(sys.path)\n\nimport utilities.math as math\n\nprint(math.add(1, 2))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n$ python3.7 my_script.py \n&#x5B;&#039;\/Users\/pankaj&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python37.zip&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7\/lib-dynload&#039;, &#039;\/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.7\/site-packages&#039;, &#039;\/Users\/pankaj\/Desktop\/python-packages&#039;]\nutilities package initialized\n3\n$ \n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Packages in Python allow us to divide our application modules and scripts into logical modules. This keeps our code base clean and easy to maintain.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3.8\/tutorial\/modules.html#packages\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Python.org Docs (opens in a new tab)\">Python.org Docs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python packages help us to manage modules and python scripts. They are normal directories with a init script &#8211; __init__.py. How to Create a Python Package? We can create a package by following below steps. Create the package directory &#8211; we can use terminal or Python IDE for this. Create __init__.py file &#8211; this is [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":67387,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-390","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=390"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/390\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/67387"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}