I think the problem is in the way Emacs' python-mode runs Python. If I type M-x run-python
, then I see this:
>>> import sys
>>> '' in sys.path
False
>>>
whereas if I run the python interpreter from the shell, I see:
>>> import sys
>>> '' in sys.path
True
>>>
This seems to be due to the following code in run-python
from progmodes/python.el:
(let* ((cmdlist
(append (python-args-to-list cmd)
'("-i" "-c" "import sys; sys.path.remove('')")))
which has no comment, and the following helpful ChangeLog entry:
2008-08-24 Romain Francoise <[email protected]>
* progmodes/python.el (run-python): Remove '' from sys.path.
I would say this is a bug in Emacs. Here's a workaround that you could put in your .emacs file:
(defun python-reinstate-current-directory ()
"When running Python, add the current directory ('') to the head of sys.path.
For reasons unexplained, run-python passes arguments to the
interpreter that explicitly remove '' from sys.path. This means
that, for example, using `python-send-buffer' in a buffer
visiting a module's code will fail to find other modules in the
same directory.
Adding this function to `inferior-python-mode-hook' reinstates
the current directory in Python's search path."
(python-send-string "sys.path[0:0] = ['']"))
(add-hook 'inferior-python-mode-hook 'python-reinstate-current-directory)