简体   繁体   中英

How to force python to ignore or recreate pyc/pyo files?

I'm developing an scientific python application that uses multiprocessing and process pools. Sometimes I make a mistake and fork bomb myself. This causes my laptop to freeze and I need to do a hard reset. However, when I load again my python installation appears to be corrupted. I get strange errors on basic imports like import string .

I traced this down to a point where it looks like python is trying to make pyc's/pyo's for some of the system modules installed in the system directory (I am working in Windows). I can temporarily fix this problem by clicking through the installation directory, sorting by time modified and manually deleting all the pyc/pyo's created on that run based on the modified date.

Is there a way to force python to ignore any existing pyc/pyos and recreate them on a launch of the interpreter? Alternatively, is it safe to delete all the pyc/pyo objects in the system Python installation? In my case that's C:\\Python27 .

Generally the easy way to force a recompile is to delete .pyc files but in the case of system files you may not have the .py so use extreme care.

Your best bet might be to create a virtual environment as a sandbox and to protect your files, you can easily recreate it as needed.

If you are already running from such then possibly your tasks are not you should check this.

I would also strongly suggest running tests like that as a restricted user rather than as admin or as a power user with admin rights, this may make a little more work at first to get it running without admin rights but has a couple of advantages:

  1. Safety - limited users have to work really hard to corrupt the system, ie if admin has installed python for all users a limited user should not have write access to the install directory,
  2. Deployment - if you pass your code on to other users they may not have admin rights but you will know that your code will run without them, (given the prerequisites),
  3. Portability - if you would like to run on Linux boxes your code should not need admin rights to run,
  4. Scalability - If you get the chance to run your code on a supercomputer/cluster I can just about guarantee it will not be run as admin.

You could touch all your .py's, and then force an import.

But why do you want to do this?

One main use case I see in addition is when you work out of a git repository and switch forth and back on branches. .py files might get checked out with different content but with older time stamp. There you have an issue if you don't force the re-creation of the .pyc files one way or the other.

CKol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM