I'm trying to use Emacs as a python editor and it works fine when I evaluate(C-c C-c) only single files but when I evaluate a file that imports another file in the same directory, I get an error saying that the file could not be imported.
Does anyone know of a workaround?
Thanks in advance
edit: Btw, i'm using Emacs23 on a Ubuntu machine.
The error was,
ImportError: No module named hlutils
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)
Since the answers previous to this one were posted an option was made available to change the default behaviour (removing the current directory from the path) to include the cwd in the path.
So a simple
(setq python-remove-cwd-from-path nil)
in your .emacs should fix this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With