4

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)

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 ?

--EDIT--

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()
jamk
  • 836
  • 1
  • 10
  • 24

3 Answers3

3

I added the following to my pre-execute script in Preferences -> PyDev -> Interactive Console -> Initial Commands:

import os
import inspect

__old_runfile = runfile

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

This overrides the default runfile command and causes Ctrl+Alt+Enter to change to the directory of the script upon loading it.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
ilintar
  • 89
  • 1
2

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)

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()
M Gaulin
  • 36
  • 4
  • I use the following now: 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() but your version is also very good – jamk Feb 12 '13 at 16:32
0

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).

  1. 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).
  2. 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".)

ptrj
  • 5,152
  • 18
  • 31
  • I'm getting `The system cannot find the file specified: '${project_loc}'` – ipetrik Aug 03 '18 at 23:04
  • 1
    @ipetrik I think it works only if you have a project opened and start "Console for currently active editor". Alternatively, you can define and use ${workspace_loc} which is the path to the workspace. – ptrj Aug 14 '18 at 02:36