If I am editing a python file, and start a python shell using elpy-shell-switch-to-shell the process appears to set the working directory as the containing directory of the file.

However, the file itself is in a subfolder of a git repo. So, I would expect the working directory to be that of the containing .git directory.

I set the project root directory using elpy-set-project-root but that didn't seem to change the behaviour of elpy-shell-switch-to-shell.

What am I doing wrong here?

  • Note that vc-root-dir can give you the root of whatever version control repository you are in. I'm not familiar with elpy-switch-to-shell, but you can probably create a elpy-switch-to-shell-in-root that automatically opens the root dir (Alan's answer) before running the elpy command
    – KevinG
    Jul 3 '17 at 23:47

This should be fixed as of the docs

By default, Elpy tries to find the root directory of the current project (git, svn or hg repository, python package or projectile project) and starts the python interpreter here. This behaviour can be suppressed with the option elpy-shell-use-project-root.

The order seems to be projectile root, then python project, then git, then mercurial, then svn. Customize elpy-project-root-finder-functions to disable some of these. (Maybe you have a setup.py in that path...). See Elpy starts Python processes at the root of my git tree, not the module's actual location for the opposite problem


You can temporarily change the default root before switching to the elpy shell:

(defun elpy-shell-switch-to-shell-in-root ()
    "Switch to python shell starting in project root"
  (let ((default-directory (elpy-project-root)))

In elpy 1.32.0

(setq elpy-shell-starting-directory 'current-directory) ;; default is 'project-root 

worked for me.

ref: Documentation


Generally Emacs will run any command in the same directory as the open file. You can open the root directory of your project in dired and run the python shell from there:

C-xC-f /path/to/dir/ RET M-x elpy-shell-switch-to-shell RET

It doesn't look like elpy provides any special way of doing this.


After posting my comment, I decided that something similar would be useful to me in general, so I wrote a little function that opens a multi-term in the root dir of a project.

(defun open-mterm-in-project-root ()
  "Opens a multi-term in the current vc project's root dir"
    (find-file (vc-root-dir))
    (call-interactively 'multi-term)))

You can probably replace the multi-term with elpy-shell-switch-to-shell and have some success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.