Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'debug': 0.03; '"""': 0.07; 'happily': 0.07; 'imports': 0.07; 'python': 0.08; '%s"': 0.09; 'files:': 0.09; 'function:': 0.09; 'none)': 0.09; 'to:addr:comp.lang.python': 0.09; 'files.': 0.09; 'def': 0.13; 'subject:file': 0.13; 'cc:addr:python-list': 0.15; 'received:74.125.82.44': 0.15; 'received:mail-ww0-f44.google.com': 0.15; '(lambda': 0.16; '.py': 0.16; 'dirs,': 0.16; 'root,': 0.16; 'roy': 0.16; 'simulate': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'globally': 0.23; 'module,': 0.23; 'ignore': 0.24; 'subject:use': 0.24; 'module': 0.26; 'pm,': 0.26; 'import': 0.27; 'variable': 0.27; 'cc:addr:python.org': 0.29; 'environment': 0.30; 'exists,': 0.30; 'usually': 0.30; 'list': 0.32; 'message-id:@gmail.com': 0.33; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'certain': 0.34; 'file.': 0.34; 'anything': 0.34; 'received:74.125.82': 0.34; "i'll": 0.35; 'file': 0.35; 'cc:2**1': 0.36; 'but': 0.37; 'received:74.125': 0.37; 'unless': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'some': 0.38; 'subject:with': 0.38; 'else': 0.39; 'files': 0.39; 'subject:: ': 0.39; 'join': 0.61; 'leading': 0.62; 'failures.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=SCfSt93rh2pHOuN8JkP388n0YOK7De8ZupXFIo88m2E=; b=CzdH48P6v0kyZ/XqmgW6g7Iqc5DlIUG7BNqV6eouhsYQPeGFPdbDELDc2c0DnMHb+T Is366ZtwcYHdf8nPR4ENaArpa2z53ypTMPrV99XaWyMbwHA8bhZPo+V4s1oAmynvs/Ln 4xmuvvlXddHHncQxQonlLrn9dS6DyJOFr7Te0= Date: Tue, 31 Jan 2012 11:18:47 +0000 From: Andrea Crotti User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111224 Thunderbird/9.0.1 MIME-Version: 1.0 To: comp.lang.python@googlegroups.com Subject: Re: Disable use of pyc file with no matching py file References: <12592360.1754.1327959045517.JavaMail.geo-discussion-forums@vby1> In-Reply-To: <12592360.1754.1327959045517.JavaMail.geo-discussion-forums@vby1> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org, Roy Smith X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328008733 news.xs4all.nl 6857 [2001:888:2000:d::a6]:36244 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19627 On 01/30/2012 09:30 PM, Roy Smith wrote: > Every so often (typically when refactoring), I'll remove a .py file and forget to remove the corresponding .pyc file. If I then import the module, python finds the orphaned .pyc and happily imports it. Usually leading to confusing and hard to debug failures. > > Is there some way to globally tell python, "Never import a .pyc unless the corresponding .py file exits"? Perhaps some environment variable I could set in my .login file? If you want to stay python only I have this function: def clean_orphaned_pycs(directory, simulate=False): """Remove all .pyc files without a correspondent module and return the list of the removed files. If simulate=True don't remove anything but still return the list of pycs """ exists, join = path.exists, path.join rm = (lambda _: None) if simulate else remove removed = [] for root, dirs, files in walk(directory): for f in files: if f.endswith(".pyc"): pyc = join(root, f) if not exists(pyc[:-1]): logger.debug("DELETING orphaned file %s" % pyc) removed.append(pyc) rm(pyc) # ignore certain sub-dirs for i in reversed(range(len(dirs))): d = dirs[i] if d == ".svn" or d.endswith(".egg-info"): dirs.pop(i) # dirs.remove(d) return removed