LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Can .pyc and .pyo files be deleted? (https://www.linuxquestions.org/questions/linux-server-73/can-pyc-and-pyo-files-be-deleted-859753/)

robertjinx 01-31-2011 06:24 AM

Can .pyc and .pyo files be deleted?
 
Hello, I have a lot of .pyc and .pyo files and I really need the space. I was wonder if I can remove them as I know python will create them automatically.

At the moment, I need to make sure I have the space and only this files I can delete... should this be fine?

Also I know they are recreated, but not sure how safe it is just to remove them.

prodev05 01-31-2011 11:06 PM

No don't do it. Remove the old kernel and old modules. That will give you enough space. Which partition is populated ? Could you pl give me the output.

robertjinx 02-01-2011 01:12 AM

I need to know if they are safe to delete or not, if not why they are not safe to delete, don't tell me about crap which I didnt ask.

Julian Andrews 02-08-2011 05:44 PM

My understanding is that as long as python can find the .py files it will automatically compile .pyo and .pyc files as needed (on module import). I know for sure that for any .pyo/.pyc files for your own python code you can erase them completely safely, I do that all the time.

If you're talking about the system files, I would be a little careful, and at least make sure that the .py file exists for any .pyo/.pyc file you want to erase. I just ran a test where I moved /usr/lib/python2.6/xmllib.pyc and ran python and imported xmllib(I chose the module totally at random) and the .pyc file wasn't recreated, but the module imported fine. I did the same thing as root, and the .pyc file was recreated, so this would suggest that the process is fairly safe, at least if you have the .py files.

As a side note, you can use the -B command line argument to python, or
Code:

sys.dont_write_bytecode = True
if you want your code not to write bytecode for imported modules. This leads to the obvious performance penalty.

Anyway, I can't make any guarantees, but my quick test suggests you should be fine. I might run a similar test with a .pyo file just to be paranoid - and of course don't erase any bytecode files if you don't have th corresponding .py file somewhere! Also, don't expect to free up much space this way! I checked on my own system and across two python installs (one on windows and one in linux) all my .pyc and .pyo files combined add up to less than 100M!

prodev05 02-08-2011 11:04 PM

Quote:

Originally Posted by Julian Andrews (Post 4252445)
My understanding is that as long as python can find the .py files it will automatically compile .pyo and .pyc files as needed (on module import). I know for sure that for any .pyo/.pyc files for your own python code you can erase them completely safely, I do that all the time.

If you're talking about the system files, I would be a little careful, and at least make sure that the .py file exists for any .pyo/.pyc file you want to erase. I just ran a test where I moved /usr/lib/python2.6/xmllib.pyc and ran python and imported xmllib(I chose the module totally at random) and the .pyc file wasn't recreated, but the module imported fine. I did the same thing as root, and the .pyc file was recreated, so this would suggest that the process is fairly safe, at least if you have the .py files.

As a side note, you can use the -B command line argument to python, or
Code:

sys.dont_write_bytecode = True
if you want your code not to write bytecode for imported modules. This leads to the obvious performance penalty.

Anyway, I can't make any guarantees, but my quick test suggests you should be fine. I might run a similar test with a .pyo file just to be paranoid - and of course don't erase any bytecode files if you don't have th corresponding .py file somewhere! Also, don't expect to free up much space this way! I checked on my own system and across two python installs (one on windows and one in linux) all my .pyc and .pyo files combined add up to less than 100M!

wonderful explanation :)

robertjinx 02-09-2011 02:59 PM

Less than 100M is fine.. and thanks, I will test it :)


All times are GMT -5. The time now is 05:06 PM.