{"id":8571,"date":"2018-01-19T07:00:00","date_gmt":"2018-01-19T07:00:00","guid":{"rendered":""},"modified":"2026-04-18T22:26:28","modified_gmt":"2026-04-18T22:26:28","slug":"python-gzip-has-no-attribute-open","status":"publish","type":"post","link":"https:\/\/hellem.org\/wblog\/?p=8571","title":{"rendered":"Python &#8211; gzip has no attribute open"},"content":{"rendered":"<p>Did try the following<br \/>\n<code><br \/>\nimport gzip<br \/>\nf = gzip.open('myfile.gz','rb')<br \/>\n<\/code><br \/>\nBut then got the error <\/p>\n<pre>AttributeError: 'module' object has no attribute 'open'<\/pre>\n<p>What I really should have done was to read the whole error stack <\/p>\n<pre>Traceback (most recent call last):\r\n  File \"gzip.py\", line 1, in module\r\n    import gzip\r\n  File \"d:\\test-py\\gzip.py\", line 13, in \r\n    fileHandler = gzip.open(pathToGzFile)\r\nAttributeError: 'module' object has no attribute 'open'module<\/pre>\n<p>While testing I had named my file <\/p>\n<pre>gzip.py<\/pre>\n<p> causing Python to believe that I meant &#8220;import the script I am testing&#8221;. Renamed the file to testgzip.py and it all works fine (name can be whatever, but do not name the file the same as a module you are importing)<\/p>\n<p>During this I found a nice way to pretty print (pprint) the attributes of a module\/object in Python, see <a href=\"https:\/\/stackoverflow.com\/a\/192365\/512139\">StackOverflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did try the following import gzip f = gzip.open(&#8216;myfile.gz&#8217;,&#8217;rb&#8217;) But then got the error AttributeError: &#8216;module&#8217; object has no attribute &#8216;open&#8217; What I really should have done was to read the whole error stack Traceback (most recent call last): File &#8220;gzip.py&#8221;, line 1, in module import gzip File &#8220;d:\\test-py\\gzip.py&#8221;, line 13, in fileHandler = gzip.open(pathToGzFile) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-8571","post","type-post","status-publish","format-standard","hentry","category-work-geek"],"_links":{"self":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/8571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8571"}],"version-history":[{"count":1,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/8571\/revisions"}],"predecessor-version":[{"id":9065,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=\/wp\/v2\/posts\/8571\/revisions\/9065"}],"wp:attachment":[{"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellem.org\/wblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}