tags:

views:

157

answers:

4

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 
A: 

Open a terminal and type emacs -q. This starts an emacs without loading the init file (.emacs). Does the same behavior occur? If not, then it means there is a problem in your .emacs file.

Edit:

When you run a script using C-c C-c from within Emacs' Python mode, the directory containing the script is not added to the beginning of sys.path. I found this out by putting

import sys
print sys.path

at the top of a test script. This is why Python is not finding other modules that reside in the same directory.

When you run the script from the command-line, Python does add the directory containing the script to sys.path. That's why the same script works from the command-line.

You can fix this by editing your ~/.bashrc file and adding

export PYTHONPATH=/path/to/Python/scripts

After saving ~/.bashrc, close emacs and start it again to make sure the change to .bashrc becomes effective.

unutbu
nope, that didn't work
smith
Is the scripts directory the directory to my file or is it a special folder in python?i tried path/to/python as /usr/share/python but it didnt work.sorry am new to linux, in windows i know that my python folder is in c:/python25, but linux has many folders named python hence i can't make out the correct one
smith
@smith, sorry I should have been more clear. Change `/path/to/Python/scripts` to the directory that contains `hlutils.py`.
unutbu
hiya, i edited the .bashrc file but it still wouldn't work even after trying some alternatives like PYTHONPATH=$PYTHONPATH:/home/desktop/project export PYTHONPATH .My fate i guess :( . Thanks a lot for your suggestions and patience, on the bright side I learnt something about my folder structure :)
smith
Hm, sorry then. I thought I had this one nailed... :-/
unutbu
A: 

I am really quite bad with emacs lisp, and I have never used Ubuntu, so the following may require some tweaking, but if the code runs fine from the shell, why not just set emacs to run the shell command? Just put:

(defun shell-compile ()
  (interactive)
  (shell-command (concat "python " (buffer-file-name))))

(add-hook 'python-mode-hook
          (lambda () (local-set-key (kbd "\C-c\C-c") 'shell-compile)))

into your .emacs file. There must be much cleaner ways to do it, but this will at least get you working.

Nikwin
Thanks, this works good. But instead of executing a shell command if it starts a python interpreter and sends the buffer contents to the python interpreter it would be great, thats the one feature that python-mode offers that i really want, just can't get it to work ;)
smith
+1  A: 

i import local modules from the same directiory just fine when using py-execute-buffer.

your problem might be related to the way py-execute-buffer goes about its work: it saves the buffer to a tmp file (in the directory py-temp-directory) and then it passes this file to python. in your case the system might be affected by the temp file directory.

another aspect: py-execute-buffer does different things with the tmp file depending whether a pyhton interpreter buffer does exist or does not: try your case with a running interperter and with out (just kill the interpreter buffer).

halloleo
+4  A: 

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)
Jim Blandy