Some problems about configuring pydev console in eclipse.

I’m planning to do some java coding now. So I installed eclipse which allows me to work with both python and java. However I was really annoyed at configuring the pydev console.
Firstly, I found I can not interact with the console if I run the script with “run” button. I must activate an interactive console to do some further interaction. But the pydev’s console behaves in an odd manner, at least, that’s how it seems to me.
The interactive console can not import modules right there in the same directory with the current working script. The solution is to add the project’s directory to pydev’s PYTHONPATH: right click project name –>PyDev–>set as source folder (add to PYTHONPATH).
Then comes the next problem. I have two similar projects. Both of them rely on a ui.py script of its own. To run these projects, I have to add their directory to Python path as described above. Then I ran project1, everything is fine. Then I ran project2, some error occured. It turns out that project2 called ui.py(1) insteat of its own ui.py. Obviously, the pythonpath configuration does not work in my situation. I have to try another solution, that is to add the projects’s directry to their external lib.
But I still can’t get my project to run correctly. This time, the console failed to find a xls file which was supposed to be read. I typed import os; print(os.getcwd()) to see the current working directory. It turns out that the interactive console was working under eclipse’s default working path rather than the project’s path. So, I have to edit the initial command of interactive console to force the console to change it’s working directory at start up. Go to Windows–>preferences–>Pydev–>interactive console–>initial command, change the initial interpreter commands to:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os
import inspect

__old_runfile = runfile

def runfile(file):
curpath = os.path.dirname(os.path.abspath(file))
os.chdir(curpath)
__old_runfile(file)

Now I can properly run my project. I have solved all the problems though, I think I will not use eclipse a lot on python coding. I’d still like to use spyder which is far more cute than eclipse especially in virtual environment. It really saved you a lot of time from configuring all these paths and resourses

Leave a Reply