Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 286
ChrisArmstrong Avatar asked Dec 21 '12 22:12

ChrisArmstrong


People also ask

How do I use PyDev console?

To use it, do Ctrl+Alt+Enter (while in the PyDev editor) to: Open a console if there's no open console. Make an runfile of the current editor in the console (if no text is selected), so that its symbols are available for further experimentation.

How to import PyDev?

How do I import existing projects/sources into PyDev? If the project was already in Eclipse (i.e.: has the . project and . pydevproject files), use File > Import > Existing Projects into Workspace.

How to import PyDev in Eclipse?

After opening Eclipse, go to the menu: File > New > Project > PyDev > PyDev project. You should see the screen below: Project name: The name of the project.

How to remove PyDev from Eclipse?

Eclipse 3.5 onwards If you installed with the update site, go to the menu help > about > installation details then on the Installed Software tab, select the plugins you want to uninstall and click Uninstall. If you installed with the zip file, just remove the com. python. pydev and org.


2 Answers

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.

like image 159
boulderZ Avatar answered Oct 20 '22 03:10

boulderZ


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.

like image 32
Fishbanter Avatar answered Oct 20 '22 01:10

Fishbanter