{"id":214,"date":"2019-12-26T23:22:00","date_gmt":"2019-12-26T23:22:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:52","modified_gmt":"2024-09-13T15:04:52","slug":"python-tutorial-series-packages","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/","title":{"rendered":"Python Tutorial Series &#8211; Packages"},"content":{"rendered":"<p>Python modules can be organized into packages. A package is a namespace which contains a set of modules. Packages are useful for organizing modules, especially when an application is large and contains a lot of modules. To create a package, create a directory and add python files to it. The directory must contain an __init__.py file in order to be recognized as a package.<\/p>\n<p>Example:<\/p>\n<pre><\/pre>\n<p>\npkg1<br \/>\n&nbsp; __init__.py<br \/>\n&nbsp; mod1.py<br \/>\n&nbsp; mod2.py<br \/>\napp.py<\/p>\n<p>\nModules, which are part of a package, can then be imported using the dot notation:<\/p>\n<pre><\/pre>\n<p>\nimport pkg1.mod1<\/p>\n<p>\nModules can also be imported as follows:<\/p>\n<pre><\/pre>\n<p>\nfrom pkg1 import mod1<\/p>\n<p>\nSpecific module members can be directly imported:<\/p>\n<pre><\/pre>\n<p>\nfrom pkg1.mod1 import do_something<\/p>\n<p>or<\/p>\n<p>from pkg1.mod1 import do_something as do<\/p>\n<p>\nThe __init__.py file defines package initialization code. It can be empty or define a list of modules which are imported on initialization. This can be done individually:<\/p>\n<pre><\/pre>\n<p>\nimport pkg1.mod1<br \/>\nimport pkg1.mod2<\/p>\n<p>or using __all__ to define a list of modules to import:<\/p>\n<pre><\/pre>\n<p>\n__all__ = [&#8220;mod1&#8221;, &#8220;mod2&#8221;]<\/p>\n<p>\nUsing<\/p>\n<pre><\/pre>\n<p>\nfrom pkg1 import *<\/p>\n<p>\nwould then import mod1 and mod2 at once.<\/p>\n<p>\nPackages can contain subpackages by creating sub directories:<\/p>\n<pre><\/pre>\n<p>\npkg1<br \/>\n&nbsp; __init__.py<br \/>\n&nbsp; subpkg1<br \/>\n&nbsp; &nbsp; __init__.py<br \/>\n&nbsp; &nbsp; mod1.py<br \/>\n&nbsp; &nbsp; mod2.py<br \/>\n&nbsp; subpkg2<br \/>\n&nbsp; &nbsp; __init__.py<br \/>\n&nbsp; &nbsp; moda.py<br \/>\n&nbsp; &nbsp; modb.py<\/p>\n<p>\nmoda would be imported as:<\/p>\n<pre><\/pre>\n<p>\nimport pkg1.subpkg2.moda<\/p>\n<p>\nEach subpackage also needs to contain in __init__.py file, which may or may not be empty.<\/p>\n<h4>\nFull Example:<\/h4>\n<p>app.py:<\/p>\n<pre><\/pre>\n<p>\nimport pkg1<br \/>\nfrom pkg1 import mod2<\/p>\n<p>pkg1.mod1.print_mod_name()<br \/>\nmod2.print_mod_name()<\/p>\n<p>\npkg1\/__init__.py:<\/p>\n<pre><\/pre>\n<p>\nimport pkg1.mod1<br \/>\nimport pkg1.mod2<\/p>\n<p>\npkg1\/mod1.py:<\/p>\n<pre><\/pre>\n<p>\ndef print_mod_name():<br \/>\n&nbsp; &nbsp; print(&#8216;Mod 1&#8217;)<\/p>\n<p>\npkg1\/mod2.py:<\/p>\n<pre><\/pre>\n<p>\ndef print_mod_name():<br \/>\n&nbsp; &nbsp; print(&#8216;Mod 2&#8217;)<\/p>\n<p>\n<a href=\"https:\/\/amzn.to\/2EimtFd\" rel=\"noopener\" target=\"_blank\">More Python<\/a><\/p>\n<p>\n<iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B0785Q7GSY&amp;asins=B0785Q7GSY&amp;linkId=db5e82d74b668701871debd783c9ddf6&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=B077Z55G3B&amp;asins=B077Z55G3B&amp;linkId=52c196ba1fda10ea7b99169422b21af7&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1491912057&amp;asins=1491912057&amp;linkId=e818186a630b59613fa23378eab853f9&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n<div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python modules can be organized into packages. A package is a namespace which contains a set of modules. Packages are useful for organizing modules, especially when an application is large and contains a lot of modules. To create a package, create a directory and add python files to it. The directory must contain an __init__.py [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,343,341],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","hentry","category-python","category-python-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Tutorial Series - Packages - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tutorial Series - Packages - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"Python modules can be organized into packages. A package is a namespace which contains a set of modules. Packages are useful for organizing modules, especially when an application is large and contains a lot of modules. To create a package, create a directory and add python files to it. The directory must contain an __init__.py [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-26T23:22:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:52+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"Python Tutorial Series &#8211; Packages\",\"datePublished\":\"2019-12-26T23:22:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/\"},\"wordCount\":312,\"commentCount\":0,\"articleSection\":[\"python\",\"python tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/\",\"name\":\"Python Tutorial Series - Packages - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-26T23:22:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-packages\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tutorial Series &#8211; Packages\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tutorial Series - Packages - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/","og_locale":"en_US","og_type":"article","og_title":"Python Tutorial Series - Packages - Zofxare Blog Home","og_description":"Python modules can be organized into packages. A package is a namespace which contains a set of modules. Packages are useful for organizing modules, especially when an application is large and contains a lot of modules. To create a package, create a directory and add python files to it. The directory must contain an __init__.py [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-26T23:22:00+00:00","article_modified_time":"2024-09-13T15:04:52+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"Python Tutorial Series &#8211; Packages","datePublished":"2019-12-26T23:22:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/"},"wordCount":312,"commentCount":0,"articleSection":["python","python tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/","name":"Python Tutorial Series - Packages - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-26T23:22:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-packages\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Tutorial Series &#8211; Packages"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":1,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"predecessor-version":[{"id":247,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/214\/revisions\/247"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}