PyDev note about refactoring

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.

5 comments ↓

#1 jerry chen on 05.27.06 at 12:37 am

how very observant and careful of you. it is of utmost importance to maintain a healthy amount of skepticism that the code is “okay”. ignorance is not bliss. just because it runs today doesn’t mean it will run tomorrow! i constantly code review my stuff on printouts to make sure the logic is consistent (maybe not so healthy, but i’m not burdened with deadlines, yet).

i don’t know about you, but to me, code seems to decay with time. as i develop better coding practices, or different styles, old code that seemed to run fine at one time look absolutely horrible now. my average code decay rate is 4 months from fresh to rot, it has steadied a bit in recent years.

of course, it’s a different story with unit tests. however, it is still possible to subconsciously forge “convenient” and “easy” special case tests and be complacent and overly optimistic about things.

#2 Nick on 05.27.06 at 11:45 am

Yeah, I’m “once bitten, twice shy” when it comes to messing with good code. I like the benefits of refactoring, but I’ve got to have that warm and fuzzy that everything still works.

I hear you about the code rot, I’m not sure what my timeline is though. I would guess around 6 months or so. I think a lot of it also depends on how many other projects I’m working on at the same time. :) That’s why I’ve always been big on comments, and I’m now become big on tests. One thing to describe the intent, another to test it.

-Nick

#3 Fabio Zadrozny on 05.29.06 at 9:22 am

Hi, actually, it does delete the .pyc files here… It just does not do it if your pythonpath is not correctly configured (I’d recommend checking the getting started manual to see if all is correctly configured: http://www.fabioz.com/pydev/manual_101_root.html)

So, basically, .pyc files from .py files that are not found to be in the pythonpath are not erased (but otherwise they are).

If you’re able to reproduce this when the file is in your pythonpath, please report a bug with the details for reproducing it.

Cheers,

Fabio

#4 Nick on 05.29.06 at 10:18 am

Hi Fabio, thanks for your comment!

Yes, when I first wrote this posting, the pythonpath was not set. I have since set the pythonpath, but when I repeated my test it still didn’t delete the pyc files.

I’m sending you an email detailing what I did.

-Nick

#5 » Thanks! - Possibility And Probability on 05.29.06 at 3:50 pm

[…] The other day I posted about problem I was having while refactoring some python code while using the Pydev plugin for Eclipse. Fabio Zadrozny (one of the developers for Pydev) saw my post and asked for some more details on my problem. After a few emails he had a work around for the issue. Thanks Fabio! […]

You must log in to post a comment.