简体   繁体   English

设置Pydev交互式控制台的默认目录?

[英]Set default directory of Pydev interactive console?

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. 交互式控制台(又名PyDev控制台),我使用它来运行带有Control + Alt + Enter脚本,并使用C:\\Program Files (x86)\\eclipse作为默认目录。 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 活动编辑器的pydev控制台路径

https://superuser.com/questions/486759/how-can-i-select-a-default-interactive-console-in-pydev 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 . 我还按照此处的建议将工作目录添加到了PYTHONPATH中。

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. 您链接到的脚本出现在我的搜索中,但是直到我意识到它是针对Python 2.6的并且我猜您正在使用其他版本时才起作用。

I edited Initial interpreter commands under Preferences > Pydev > Interactive Console to: 我在“首选项”>“ Pydev”>“交互式控制台”下将“初始解释器”命令编辑为:

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. 每次到达鼠标都会很快变得非常老。

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: 我将文件夹路径附加到sys.path上,这很好用,在“首选项”>“ Pydev”>“ Interactive Console”下,在第二行中添加项目文件夹路径:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
sys.path.append('F:\\projects\\python')

PyDev->Window->Preferences->PyDev->Interactive Console -> Initial Commands: PyDev->窗口->首选项-> PyDev->交互式控制台->初始命令:

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

worked for me 为我工作

This is similar to pydev console path for the active editor 这类似于活动编辑器的pydev控制台路径

The answer given in the above link by https://stackoverflow.com/users/5717589/ptrj is correct and I believe also applies here. https://stackoverflow.com/users/5717589/ptrj在上述链接中给出的答案是正确的,我相信这里也适用。 It is similar to what https://stackoverflow.com/users/5618245/daniel posted but I think gives more detailed information. 它类似于https://stackoverflow.com/users/5618245/daniel发布的内容,但我认为它提供了更详细的信息。 I will paste again here for convenience. 为了方便起见,我将再次粘贴在这里。 ptrj should get credit. ptrj应该得到信誉。 Also, ptrj answer for the above link should have been upvoted IMO. 此外,以上链接的ptrj答案应该已被IMO赞成。

from ptrj: I had the same problem and have just found another solution. 来自ptrj:我遇到了同样的问题,并且刚刚找到了另一个解决方案。 It's similar to the ones already mentioned but fully configurable within eclipse/pydev (no need to modify your scripts). 它与已经提到的相似,但是可以在eclipse / pydev中完全配置(无需修改脚本)。

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). 在“窗口”->“首选项”->“ PyDev”->“解释器”->“ Python解释器”中,选择“环境”选项卡,并添加一个新的变量,其名称为PROJECT_PATH(或您选择的任何值),其值为$ {project_loc}(这是项目文件夹的绝对路径) 。 Then in Window -> Preferences -> PyDev -> Interactive Console -> Initial Command add line import os; 然后在Window-> Preferences-> PyDev-> Interactive Console-> Initial Command中添加行import os; os.chdir(os.environ['PROJECT_PATH']). 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. 我使用这种方法,它对我有用。

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. 我使用的是Python 2.7.3,Pydev 2.7.0。
My current sys.path is: 我当前的sys.path是:

['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. 作为@ChrisArmstrong解决方案,这很难。
So I use Ipython alone as the alternative. 所以我单独使用Ipython作为替代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM