{"id":4067,"date":"2015-10-27T07:46:19","date_gmt":"2015-10-27T15:46:19","guid":{"rendered":"https:\/\/blog.mozilla.org\/webdev\/?p=4067"},"modified":"2015-10-27T07:46:19","modified_gmt":"2015-10-27T15:46:19","slug":"eradicating-those-nasty-pyc-files","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/","title":{"rendered":"Eradicating those nasty .pyc files"},"content":{"rendered":"<p>I recently acquired a new development laptop and moved a number of local Git repositories from my old machine to my new machine. In doing so I also changed the folder structure, and when trying to run some code I was presented with this Python error:<\/p>\n<p><code><br \/>\nimport file mismatch:<br \/>\nimported module 'tests.desktop.consumer_pages.test_details_page' has this __file__ attribute:<br \/>\n\/Users\/bsilverberg\/gitRepos\/marketplace-tests\/tests\/desktop\/consumer_pages\/test_details_page.py<br \/>\nwhich is not the same as the test file we want to collect:<br \/>\n\/Users\/bsilverberg\/Documents\/gitRepos\/marketplace-tests\/tests\/desktop\/consumer_pages\/test_details_page.py<br \/>\nHINT: remove __pycache__ \/ .pyc files and\/or use a unique basename for your test file modules<br \/>\n<\/code><\/p>\n<p>This was a symptom of the fact that Python creates <code>.pyc<\/code> files on my machine when it compiles code. This can result in other nastiness too, as well as cluttering up your machine, so I wanted to both delete all of these files and also prevent Python from doing it in the future. This post contains info on how to do both.<\/p>\n<h3>Deleting all .pyc files from a folder<\/h3>\n<p>You can use the <code>find<\/code> command (on OS X and Linux) to locate all of the <code>.pyc<\/code> files, and then use its <code>delete<\/code> option to delete them.<\/p>\n<p>The command to find all <code>.pyc<\/code> files in all folders, starting with the current one is:<br \/>\n<code>find . -name '*.pyc'<\/code><\/p>\n<p>If you want to delete all the files found, just add the <code>-delete<\/code> option:<br \/>\n<code>find . -name '*.pyc' -delete<\/code><\/p>\n<p>Obviously, this can be used for any file type that you wish to eradicate, not just <code>.pyc<\/code> files.<\/p>\n<h3>Preventing Python from writing .pyc files<\/h3>\n<p>I don&#8217;t like having all of those extra files cluttering my machine, and, in addition to the error I mentioned above, I have from time to time seen other errors related to out of date <code>.pyc<\/code> files.<\/p>\n<p>Another issue that <code>.pyc<\/code> files can cause is that they can be orphaned, for example if you remove a <code>.py<\/code> file from your project, but the <code>.pyc<\/code> file remains (which can happen as one often adds <code>*.pyc<\/code> to <code>.gitignore<\/code>). Python can then still pick up the module from the <code>.pyc<\/code> file via an <code>import<\/code> which can lead to difficult to diagnose bugs.<\/p>\n<p>For these reasons I want to prevent Python from ever writing those files again. To do this all you have to do is set the environment variable <code>PYTHONDONTWRITEBYTECODE<\/code> to <code>1<\/code>. You can ensure that that variable is set for any bash session that you start by adding the following to your <code>.bash_profile<\/code> or <code>.bashrc<\/code>:<br \/>\n<code>export PYTHONDONTWRITEBYTECODE=1<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently acquired a new development laptop and moved a number of local Git repositories from my old machine to my new machine. In doing so I also changed the folder structure, and when trying to run some code I &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/\">Continue reading<\/a><\/p>\n","protected":false},"author":968,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[],"coauthors":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Eradicating those nasty .pyc files - Mozilla Web Development<\/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:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Silverberg\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/\",\"name\":\"Eradicating those nasty .pyc files - Mozilla Web Development\",\"isPartOf\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\"},\"datePublished\":\"2015-10-27T15:46:19+00:00\",\"dateModified\":\"2015-10-27T15:46:19+00:00\",\"author\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/9c71cb8972b2b32c4e0e99ede7dda544\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.mozilla.org\/webdev\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Eradicating those nasty .pyc files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/\",\"name\":\"Mozilla Web Development\",\"description\":\"For make benefit of glorious tubes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/9c71cb8972b2b32c4e0e99ede7dda544\",\"name\":\"Bob Silverberg\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/57edf28f78ce35b8dbae314b80b97091\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1958743ac34aca5c4eaccda81833f54a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1958743ac34aca5c4eaccda81833f54a?s=96&d=mm&r=g\",\"caption\":\"Bob Silverberg\"},\"url\":\"https:\/\/blog.mozilla.org\/webdev\/author\/bsilverbergmozilla-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Eradicating those nasty .pyc files - Mozilla Web Development","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:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/","twitter_misc":{"Written by":"Bob Silverberg","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/","url":"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/","name":"Eradicating those nasty .pyc files - Mozilla Web Development","isPartOf":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#website"},"datePublished":"2015-10-27T15:46:19+00:00","dateModified":"2015-10-27T15:46:19+00:00","author":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/9c71cb8972b2b32c4e0e99ede7dda544"},"breadcrumb":{"@id":"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.mozilla.org\/webdev\/2015\/10\/27\/eradicating-those-nasty-pyc-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.mozilla.org\/webdev\/"},{"@type":"ListItem","position":2,"name":"Eradicating those nasty .pyc files"}]},{"@type":"WebSite","@id":"https:\/\/blog.mozilla.org\/webdev\/#website","url":"https:\/\/blog.mozilla.org\/webdev\/","name":"Mozilla Web Development","description":"For make benefit of glorious tubes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/9c71cb8972b2b32c4e0e99ede7dda544","name":"Bob Silverberg","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/57edf28f78ce35b8dbae314b80b97091","url":"https:\/\/secure.gravatar.com\/avatar\/1958743ac34aca5c4eaccda81833f54a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1958743ac34aca5c4eaccda81833f54a?s=96&d=mm&r=g","caption":"Bob Silverberg"},"url":"https:\/\/blog.mozilla.org\/webdev\/author\/bsilverbergmozilla-com\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/4067"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/users\/968"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/comments?post=4067"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/4067\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/media?parent=4067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/categories?post=4067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/tags?post=4067"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/coauthors?post=4067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}