10

The interactive console (aka PyDev console) which I use to run scripts with Control + Alt + Enter loads with C:\Program Files (x86)\eclipse as the default directory. How can I make it load to the default working directory that the script or project is located in?

I've been researching this all over now and nothing seems to work. It looks like others have been having the same issues with no answers too:

pydev console path for the active editor

https://superuser.com/questions/486759/how-can-i-select-a-default-interactive-console-in-pydev

I also tried implementing a custom startup script found here to no avail. I've also added my working directory to the PYTHONPATH as suggested here.

Community
  • 1
  • 1
ChrisArmstrong
  • 2,491
  • 8
  • 37
  • 60

5 Answers5

2

This is similar to pydev console path for the active editor

The answer given in the above link by https://stackoverflow.com/users/5717589/ptrj is correct and I believe also applies here. It is similar to what https://stackoverflow.com/users/5618245/daniel posted but I think gives more detailed information. I will paste again here for convenience. ptrj should get credit. Also, ptrj answer for the above link should have been upvoted IMO.

from ptrj: I had the same problem and have just found another solution. It's similar to the ones already mentioned but fully configurable within eclipse/pydev (no need to modify your scripts).

In Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter choose tab Environment and add a new variable with Name PROJECT_PATH (or anything of your choice) and Value ${project_loc} (this is an absolute path to the project folder). Then in Window -> Preferences -> PyDev -> Interactive Console -> Initial Command add line import os; os.chdir(os.environ['PROJECT_PATH']). (It works if you start a "Console for currently working editor".)

I use this method and it works for me.

Community
  • 1
  • 1
boulderZ
  • 21
  • 3
1

I struggled with this one as well. The script you linked to came up in my searches but never worked until I realised it was for Python 2.6 and I'm guessing you're using a different version.

I edited Initial interpreter commands under Preferences > Pydev > Interactive Console to:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os
cwd_path = [path for path in sys.path if 'org.python.pydev' not in path and 'python2' not in path]
if len(cwd_path) == 1:
    os.chdir(cwd_path[0])

(with a line break at the end) and it worked fine.

I still can't figure out a way of setting the default console type though. Reaching for the mouse each time gets very old very fast.

Fishbanter
  • 11
  • 1
  • This works for me to change the working directory. I also want to know if it is possible to set the default console type. – panc Nov 09 '16 at 14:30
1

I append the folder path to sys.path which works fine, under Preferences > Pydev > Interactive Console, add the second line with your project folder path:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
sys.path.append('F:\\projects\\python')
MauricioRobayo
  • 2,207
  • 23
  • 26
1

PyDev->Window->Preferences->PyDev->Interactive Console -> Initial Commands:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os
os.chdir('${workspace_loc:MyProject/src}')

worked for me

Daniel
  • 11
  • 2
0

Me too. Cause there is length limite on comment, I paste my comment as answer.
And I work on Python 2.7.3, Pydev 2.7.0.
My current sys.path is:

['D:\\Aptana Studio 3\\plugins\\org.python.pydev_2.7.0.2013012902\\pysrc',
'D:\\Python27\\lib\\site-packages\\distribute-0.6.30-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\pydap-3.1.rc1-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\coards-1.0.2-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\pastedeploy-1.5.0-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\pastescript-1.7.5-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\paste-1.7.5.1-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\genshi-0.6-py2.7-win32.egg',
'D:\\Python27\\lib\\site-packages\\httplib2-0.7.7-py2.7.egg',
'D:\\Python27\\lib\\site-packages\\virtualenv-1.8.4-py2.7.egg',
'D:\\Aptana Studio 3\\plugins\\org.python.pydev_2.7.0.2013012902\\pysrc',
'E:\\workspace\\pst_python',
'D:\\Python27\\DLLs',
'D:\\Python27\\lib',
'D:\\Python27\\lib\\plat-win',
'D:\\Python27\\lib\\lib-tk',
'D:\\Python27',
'D:\\Python27\\Lib\\site-packages',
'D:\\Python27\\Lib\\site-packages\\requests-0.14.2-py2.7.egg',
'D:\\Python27\\Lib\\site-packages\\extract-1.0-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'D:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info',
'D:\\Python27\\Lib\\site-packages\\IPython\\extensions']

As @ChrisArmstrong solution, it is hard.
So I use Ipython alone as the alternative.

Honghe.Wu
  • 5,899
  • 6
  • 36
  • 47