6

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?

1
  • 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
    Commented Jul 3, 2017 at 23:47

6 Answers 6

4

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

1

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"
  (interactive)
  (let ((default-directory (elpy-project-root)))
    (elpy-shell-switch-to-shell))
  )
1

In elpy 1.32.0

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

worked for me.

ref: Documentation

0

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.

0

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"
  (interactive)
  (progn
    (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.

0

Since Emacs 29 there is now a variable to start your python shell from your project directory or on a per buffer basis which is called python-shell-dedicated!

If you want to customize it you can just M-x then type customize variable and look for python-shell-dedicated. You can either set it to:

  • 'buffer to open a new python shell for each buffer,
  • 'project to start one shell per project which start in the project root if one is found,
  • nil to only open one python shell which start at the current buffer and then will be shared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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