{"id":2498,"date":"2011-02-09T09:39:04","date_gmt":"2011-02-09T17:39:04","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=2498"},"modified":"2011-02-09T09:39:04","modified_gmt":"2011-02-09T17:39:04","slug":"learning-python-the-hard-way-__init__-py-needed-for-packages-as-opposed-to-modules","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2011\/02\/09\/learning-python-the-hard-way-__init__-py-needed-for-packages-as-opposed-to-modules\/","title":{"rendered":"Learning Python the hard way: __init__.py needed for packages (as opposed to modules)"},"content":{"rendered":"<p>Just ran into this today, and wanted to get it down so I never forget: <\/p>\n<p>Modules are not packages! \ud83d\ude42<\/p>\n<p>From the docs, a <em>Module<\/em> is: <\/p>\n<blockquote><p>\nA module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module\u2019s name (as a string) is available as the value of the global variable __name__.\n<\/p><\/blockquote>\n<p>And a <em>Package<\/em> is:<\/p>\n<blockquote><p>\nPackages are a way of structuring Python\u2019s module namespace by using \u201cdotted module names\u201d.\n<\/p><\/blockquote>\n<p>When you create a module and you want other scripts to be able to reference it, you need an __init__.py in the directory containing the modules in order for Python to treat a directory as containing a package.<\/p>\n<p>The error manifests as &#8220;ImportError: No module named XXXX&#8221;. <\/p>\n<p>All I had to do was touch __init__.py and things worked. I&#8217;m sure there are other things I&#8217;m going to learn today, but that was a silly one that I didn&#8217;t expect, and wasn&#8217;t mentioned in the Modules documentation, but <a href=\"http:\/\/docs.python.org\/tutorial\/modules.html#packages\">is mentioned in Packages<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just ran into this today, and wanted to get it down so I never forget: Modules are not packages! \ud83d\ude42 From the docs, a Module is: A module is a file containing Python definitions and statements. The file name is &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2011\/02\/09\/learning-python-the-hard-way-__init__-py-needed-for-packages-as-opposed-to-modules\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[409,408,660],"class_list":["post-2498","post","type-post","status-publish","format-standard","hentry","category-python","tag-init","tag-packages","tag-python"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/comments?post=2498"}],"version-history":[{"count":4,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2498\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2498\/revisions\/2513"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=2498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=2498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=2498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}