Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Is there a way in python to delete the old .pyc before running the file

It seems that sometimes when I pull from the git the old .pyc runs instead of the new pulled .py file is there a way to automatically clear the .pyc file so it runs always the fresh version ?

like image 733
Eduard Florinescu Avatar asked Sep 24 '12 15:09

Eduard Florinescu


People also ask

How do I clear a .PYC file?

pyc files from a folder. You can use the find command (on OS X and Linux) to locate all of the . pyc files, and then use its delete option to delete them. Obviously, this can be used for any file type that you wish to eradicate, not just .

Can we delete .PYC files?

To remove Python compiled filesIn the Project tool window, right-click a project or directory, where Python compiled files should be deleted from. From the context menu, choose Clean Python compiled files. The . pyc and $py.

Can you delete __ Pycache __?

First finds all __pycache__ folders in current directory. Execute rm -r {} + to delete each folder at step above ( {} signify for placeholder and + to end the command)

What happens if I delete Pycache?

__pycache__ is a folder containing Python 3 bytecode compiled and ready to be executed. I don't recommend routinely laboriously deleting these files or suppressing creation during development as it wastes your time.


1 Answers

The old .pyc is automatically cleared by Python, provided the modified date on the .py file is newer.

You could manually delete all .pyc files in a directory structure with:

find . -name \*.pyc -delete

and Python will re-create them as modules are imported. You can also run:

python -m compileall .

to force a compilation.

like image 106
Martijn Pieters Avatar answered Sep 27 '22 22:09

Martijn Pieters