问题
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.
回答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.
回答2:
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')
回答3:
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
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/13997877/set-default-directory-of-pydev-interactive-console