Menu

how to set working directory for console?

Valery
2009-09-07
2013-03-25
  • Valery

    Valery - 2009-09-07

    Hi,

    I start an interactive console for a currently edited file.

    For some reason working directory isn't set to the directory the file is located in.
    It is set to Eclipse's installation directory:

    >>> os.system("pwd")
    /cygdrive/p/dev/eclipse3.5

    and this is also immune to the following:

      os.system("cd /path/to/the/right/directory")

    I've walked to the property entries that I've found somewhat relevant:
    Project-->Settings-->"Run/Debug Settings" --> "myproject mymodule.py" -->"Edit" -->"Arguments"-->"Working directory" --> "Other": ${workspace_loc:myproject/src}

    This setting I find OK. Why then the current dir isn't ${workspace_loc:myproject/src} ?

    How could I fix it?

    regards
    Valery

     
    • Fabio Zadrozny

      Fabio Zadrozny - 2009-09-07

      Hi,

      That's correct, right now it only gets the info for the pythonpath for the current editor, and not the working dir.

      Now, to change the current dir, if you use os.chdir(path) (instead of going for shell commands -- which won't affect the current shell, only the shell you spawned) it should work. Also, use the python api to get the dir too (os.getcwd()).

      Cheers,

      Fabio

       
    • Valery

      Valery - 2009-09-07

      Hi,

      Fabio, thanks for the solution.

      BTW, any plans to address this in upcoming releases? Would be nice... :)

      regards
      Valery

       
  • IcE

    IcE - 2010-03-11

    Hi,
    I use this little script under "Preferences/Pydev/Interactive Console/Initial Interpreter commands" to set the current path automatically to the location of your selected file. If you have custom paths in your PYTHONPATH, you have to do some workarround. If not, it should work out of the box.

    import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
    import os
    base_path, _ = os.path.split(sys.executable)
    cwd_path = [path for path in sys.path if base_path not in path 
                   and 'org.python.pydev' not in path and 'python26.zip' not in path]
    if len(cwd_path) == 1:
        os.chdir(cwd_path[0])
    

    Regards

     
    • Shawn Wang

      Shawn Wang - 2013-03-25

      Dear IcE,

      Thank you very much for your code! That worked great for me, however, it only gets me to the path of the project instead of the path to file, because only the project folder was in PYTHONPATH instead of the file folder. Would you mind giving me a little hint, on whether it's me not doing it correctly, or that's the limit to this code?

      Thanks!

      PS: attached is my code - I think this worked to make it to the project folder.

      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 'Python3' not in path and 'python33.zip' not in path]
      if len(cwd_path) == 1:
          os.chdir(cwd_path[0])
      
      
      -Shawn