1

When i do C-c C-c on a python module, it leads to *Python* buffer with Inferior Python : run Shell-Compile mode. Suppose the correct cwd is /x/y/z/
The problem is when I print os.getcwd(), i get wrong cwd = /x/y/
Due to this, modules from the same directory can not be imported.

But when i run M-x run-python from the same module, os.getcwd() is fine and modules can be imported.

Anyone knows what is happening here ?

7
  • C-c C-c (python-shell-send-buffer) sends code to the existing buffer. So I guess you'd already created a buffer in that other directory. What do you want to do? Change the directory of the existing buffer (which may have been used for different, even incompatible stuff)? Have separate Python buffers for each file? for each directory? – Gilles 'SO- stop being evil' Jun 9 '19 at 9:01
  • C-c C-c interprets the code but can not import any modules from the same directory. os.getcwd() in the same interpreter buffer gives wrong directory - this is why the modules from same directory could not be imported. Now i kill this *Python* buffer and run M-x run-python. Now os.getcwd() runs just fine. Running C-c C-c also interprets and imports all modules since cwd is correct now. Why this cwd difference between C-c C-c and M-x run-python ? – Dishant Arora Jun 9 '19 at 9:13
  • C-c C-c sends code to the existing buffer. If the existing buffer was created from a file in a different directory, the existing buffer is in that different directory. If the existing buffer was created from a file in the same directory, the existing buffer is in the same directory. – Gilles 'SO- stop being evil' Jun 9 '19 at 9:17
  • *Python* buffer doesn't exist. C-c C-c is run on a file in a directory /x/y/z/. os.getcwd() in this buffer returns /x/y/ instead of /x/y/z/. Killing this *Python* buffer, creating the new *Python* buffer with M-x run-python from the same file same directory and os.getcwd() returns correct /x/y/z/ now and modules from the same directory are getting imported. Wasn't C-c C-c also supposed to do the same thing and import the modules from the same directory ? – Dishant Arora Jun 9 '19 at 9:30
  • Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native-enable’ was t and "python" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally. Also i am getting this warning when i run C-c C-c on my python code. – Dishant Arora Jun 9 '19 at 9:36
1

If the elpy-shell-starting-directory variable is set to project-root then you will see the behaviour described. That is, the default directory for the Python shell is /x/y, the root of /x/y/z. Thus while editing code in a project z, it makes sense to run the code from y.

To get what you want, set current-directory as the value of elpy-shell-starting-directory.

So "M-x customize-variable elpy-shell-starting-directory", change to current-directory, and then apply and save for future sessions.

This will add the following to ~/.emacs-custom

 '(elpy-shell-starting-directory (quote current-directory))

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.