Possibility and Probability

A Python programmer with a personality thinking about space exploration

26 May 2006

PyDev note about refactoring

by Nick

Today I was doing some light refactoring on a python project I’m working on using Pydev and Eclipse. Over the life of the project, I’ve accumulated a lot of files that aren’t being used, have contain classes that have been moved, etc.. So today I decided to get a better handle on the project I would create a sub-dir and move the old code there while I evaluate if it is needed or not. So, as I would move a few files to the old-code directory, I would re-run the app to make sure that it could still function correctly. At first this was very easy, most of the files I moved I knew were dead and could be moved with no problem. But… I moved one file that I knew had a class I was using somewhere. I tried to run the project to to confirm that it needed that now missing class, and to my surprise the app fired up and ran with no problem. Puzzled I removed the “main” file and again was able to re-run it. It turns out that when you move a python file around in Eclipse, Pydev does not do a complete rebuild. This is the opposite of what Eclipse does with Java code, so it took me by surprise. Because nothing was being rebuilt the old .pyc files were still there making it look like everything was running ok. After manually deleting the pyc files and re-running the app, I began to get the errors I was expecting to see. So, if you are using Pydev, be sure that if you delete or move a file around you do a clean on the project otherwise you will be lulled into a false sense of security. I’m sure there’s a command-line switch for python that will force it to recompile everything every time the app is run, but I’m not sure that is a good idea. A large project could incur a large overhead. Plus since this “error” only occurs when a file is moved (which probably doesn’t happen too often once a project hits a certain stage), if a file is edited and saved, it gets recompiled automatically.