Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.getcwd() behavior on run-python vs elpy. elpy defaults to $HOME instead of directory of the file. #2034

Open
vanangamudi opened this issue Nov 9, 2023 · 0 comments

Comments

@vanangamudi
Copy link

Summary

os.getcwd() behavior on run-python vs elpy. elpy defaults to $HOME instead of directory of the file. How to make elpy to use the project root directory as the default working directory?

Steps to reproduce


import os
print(os.getcwd())

=C-c C-c= under Elpy
`

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import os
... print(os.getcwd())

/home/vanangamudi

`

=C-c C-c= under =M-x run-python=
`
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import os
... print(os.getcwd())

/home/vanangamudi/home/projects/code/astraa/assetmgmt

`

My configuration

OS

Linux kaikuttai 6.2.0-36-generic #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct 9 15:34:04 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Result of (elpy-config)

Elpy Configuration

Emacs.............: 29.1
Elpy..............: 1.35.0
Virtualenv........: None
Interactive Python: python3 3.10.12 (/usr/bin/python3)
RPC virtualenv....: usr (/usr)
 Python...........: python 3.10.12 (/usr/bin/python)
 Jedi.............: 0.18.2 (0.19.1 available)
 Autopep8.........: 1.6.0 (2.0.4 available)
 Yapf.............: 0.32.0 (0.40.2 available)
 Black............: 21.12b0 (23.11.0 available)
Syntax checker....: Not found (flake8)

Elpy configuration in my init.el

  (use-package elpy
    :ensure t
    :defer t
    :init
    (advice-add 'python-mode :before 'elpy-enable)
    :config
    (setq elpy-rpc-virtualenv-path 'current))

  ;; Enable Flycheck
  (when (require 'flycheck nil t)
    (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
    (add-hook 'elpy-mode-hook 'flycheck-mode))

  ;; Enable autopep8
  ;; (require 'py-autopep8)
  ;; (add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)

  ;; https://elpy.readthedocs.io/en/latest/customization_tips.html#enable-full-font-locking-of-inputs-in-the-python-shell
  (advice-add 'elpy-shell--insert-and-font-lock
              :around (lambda (f string face &optional no-font-lock)
                        (if (not (eq face 'comint-highlight-input))
                            (funcall f string face no-font-lock)
                          (funcall f string face t)
                          (python-shell-font-lock-post-command-hook))))

  (advice-add 'comint-send-input
              :around (lambda (f &rest args)
                        (if (eq major-mode 'inferior-python-mode)
                            (cl-letf ((g (symbol-function 'add-text-properties))
                                      ((symbol-function 'add-text-properties)
                                       (lambda (start end properties &optional object)
                                         (unless (eq (nth 3 properties) 'comint-highlight-input)
                                           (funcall g start end properties object)))))
                              (apply f args))
                          (apply f args))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant