活动编辑器的pydev控制台路径 [英] pydev console path for the active editor

查看:87
本文介绍了活动编辑器的pydev控制台路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用pydev在python中做一些脚本. 我遇到的问题是我无法在脚本所在的项目目录中启动控制台,该控制台在pydev/aptana文件夹(在aptana文件夹中的某个位置)中启动.

I want to do some scripting in python with pydev. The problem that I have is that I can't start the console in the project directory where the script is, the console starts in the pydev/aptana folder (somewhere in the aptana folder).

我可以解决这些问题,在脚本中添加以下代码:

I can solve these adding the following code to my script:

import os
import inspect
filename = inspect.getframeinfo(inspect.currentframe()).filename
curpath = os.path.dirname(os.path.abspath(filename))
os.chdir(curpath)

有人知道如何在通过pydev启动时自动将控制台的目录更改为项目位置路径吗? Ctrl + Alt + Enter 方式?

Does somebody know how to change the directory of the console to the project position path automatically when started over the pydev Ctrl+Alt+Enter way ?

-编辑-

如果有人喜欢,这是我自己的解决方案:

Here is my own solution if someone like it more:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version)) import os;os.chdir([p for p in os.environ['PYTHONPATH'].split(os.pathsep) if ('Aptana Workspace' in p)][0]) pwd()

推荐答案

我使用了一些技巧来使其正常工作.如果我理解您的问题,则希望将IPython环境中的当前工作目录设置为活动文件所在的目录.因此,如果您正在编辑D:/projects/file.py,则希望pwd()命令(在IPython中)返回D:/projects.这就是我的解决方案中被黑的部分.我所有的项目都在我的D盘上,但是所有正常的python导入都来自我的C盘上的安装位置.因此,以下内容:

I used a bit of a hack to get this working. If I understand your question, you want to have the current working directory in the IPython environment set to the directory in which your active file resides. So if you are editing D:/projects/file.py, you want the pwd() command (in IPython) to return D:/projects. This is where the hacked together part of my solution comes from. All my projects are on my D drive, but all the normal python imports come from the install location on my C drive. So the following:

os.environ['PYTHONPATH'].split(os.pathsep)

生成一个列表,其中D驱动器上只有一个路径位于我的活动文件目录中(因为PyDev将PYTHONPATH设置为包括正确的目录).如果您不使用D驱动器,那么应​​该有其他独特的方法来识别该列表中的哪些路径与您的项目有关(例如在Documents或My Documents等中).如果没有唯一标识您的项目路径的方法,则此答案不起作用.但是在"D:/"足够唯一标识符的简单情况下,这是设置(窗口>首选项> PyDev>交互式控制台)中的启动代码

results in a list on which only one path is on the D drive which is of my active file's directory (because of PyDev setting the PYTHONPATH to include the correct directory). If you don't use the D drive, then there should be some other unique way of identifying which of the paths in that list pertains to your projects (like in Documents or My Documents, etc.). If there isn't a way of uniquely identifying your project path, then this answer doesn't work. But in the simple case of "D:/" being enough of a unique identifier, this is my startup code in the settings (Window > Preferences > PyDev > Interactive Console)

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os;os.chdir([p for p in os.environ['PYTHONPATH'].split(os.pathsep) if p.startswith("D")][0])
pwd()

这篇关于活动编辑器的pydev控制台路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆