Python模式导入问题

问题描述:

我想使用Emacs作为一个Python编辑器,它工作正常,当我评估(抄送Cc)只有单个文件,但当我评估一个文件导入同一目录中的另一个文件时,我得到一个错误说该文件无法导入。Python模式导入问题

有谁知道解决方法?

在此先感谢

编辑:顺便说一句,我使用的Emacs23 Ubuntu的机器上。

错误是,

ImportError: No module named hlutils 
+0

我不是在试图不尊重你,我只是这样说,因为我从来没有这样的问题,我只是想确保你的,但是当你试图用python正常运行文件,在emacs之外,它确实运行正确,对吧? – Nikwin 2010-04-17 14:47:50

+0

此外,请尝试运行以下python代码: import os print os.listdir('。') 并查看它打印出的文件夹。 – Nikwin 2010-04-17 14:53:20

+0

是的,我在Scite中写过,它工作。我正在切换到emacs,因为它的动态性更强一些(它有一个快速测试代码的repl,而scite只是运行一个python命令并显示输出)。 不采取任何当然的进攻:) – smith 2010-04-17 14:59:15

打开终端并键入emacs -q。这会启动一个emacs而不加载init文件(.emacs)。同样的行为是否会发生?如果没有,那么这意味着你的.emacs文件有问题。

编辑:

当你使用C-C C-C从Emacs的Python的模式中运行的脚本,包含脚本的目录添加到sys.path开始。我通过在测试脚本的顶部放置

import sys 
print sys.path 

来发现这一点。这就是为什么Python没有找到驻留在同一目录中的其他模块。

当您从命令行运行脚本时,Python确实将包含脚本的目录添加到sys.path。这就是为什么相同的脚本可以从命令行运行的原因。

您可以通过编辑你的〜/ .bashrc文件并添加

export PYTHONPATH=/path/to/Python/scripts 

节省的〜/ .bashrc,靠近emacs的后解决这个问题,并再次启动它,以确保变更的.bashrc生效。

+0

nope,那不起作用 – smith 2010-04-17 14:10:46

+0

脚本目录是我的文件的目录还是python中的特殊文件夹? 我试过/到/ python作为/ usr/share/python,但它没有工作。 对不起,新的linux,在Windows中,我知道我的python文件夹是在c:/ python25,但linux有很多文件夹名为python因此我不能做出正确的一个 – smith 2010-04-17 16:47:35

+0

@smith,对不起,我应该更多明确。将'/ path/to/Python/scripts'更改为包含'hlutils.py'的目录。 – unutbu 2010-04-17 16:58:33

我对emacs lisp非常不满,而且我从未使用Ubuntu,因此以下可能需要进行一些调整,但是如果代码在shell中运行良好,为什么不直接设置emacs来运行shell命令呢?只要把:

(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))) 

到您的.emacs文件。必须有更清洁的方式来做到这一点,但这至少会让你工作。

+0

谢谢,这工作很好。但是,如果它启动一个python解释器并将缓冲区内容发送给python解释器,而不是执行一个shell命令,那么它会很好,那是python-mode提供的我真正想要的一个特性,就是无法让它工作; ) – smith 2010-04-17 15:29:14

使用py-execute-buffer时,我从同一个目录导入本地模块就好了。

您的问题可能与py-execute-buffer的工作方式有关:它将缓冲区保存到tmp文件(在目录py-temp-directory中),然后将该文件传递给Python。在你的情况下,系统可能会受到临时文件目录的影响。

另一方面:py-execute-buffer根据Python解释器缓冲区是否存在与tmp文件做了不同的事情。用正在运行的解释器试试你的情况,不要(只需要终止解释器缓冲区)。

我认为问题在于Emacs的python模式运行Python。如果键入M-x run-python,然后我看到:

>>> import sys 
>>> '' in sys.path 
False 
>>> 

而如果我运行从shell Python解释器,我看到:

>>> import sys 
>>> '' in sys.path 
True 
>>> 

这似乎是由于run-python从下面的代码progmodes/python.el:

(let* ((cmdlist 
    (append (python-args-to-list cmd) 
     '("-i" "-c" "import sys; sys.path.remove('')"))) 

其中有没有评论,下面有帮助的更新日志条目:

2008-08-24 Romain Francoise <[email protected]> 

     * progmodes/python.el (run-python): Remove '' from sys.path. 

我会说这是Emacs中的一个错误。这里有一个解决方法,你可以把你的.emacs文件:

(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) 
+1

根据http://twistedmatrix.com/pipermail/cvstoys-list/2003q3/000114.html - 这是一个“安全修复”,其他信息在这里:http://old.nabble.com/python.el: -why-remove-%27%27-from-sys.path - td22502790.html – 2011-05-23 11:02:29

我有同样的问题,如果你不想编辑您的.emacs文件,你可以在你的脚本的开头加上:

import sys 
import os 
sys.path.append(os.getcwd()) 

它会将当前工作dirrectory您sys.path中暂时

+1

注意:您可能希望将当前工作目录添加到sys.path.insert(0,os.getcwd ()) – kkurian 2012-10-13 20:51:33

因为在此之前,一个的答案被张贴在提供一个选项来改变默认的行为(从路径删除当前目录)将cwd包含在路径中。

因此,一个简单

(setq python-remove-cwd-from-path nil) 

在你的.emacs应该解决这个问题。

+0

没有为我工作 – cgl 2014-11-27 20:25:54

+0

你也可以通过'Mx自定义变量[RET] python-remove-cwd-from-path [RET]'在你的**。emacs **文件中设置这个变量。 – DavidRR 2015-01-14 16:12:38

改进现有的答案以上:EVAL这在临时缓冲区添加CWD返回到PYTHONPATH

(defun python-reinstate-current-directory() 
    (python-send-string "sys.path[0:0] = ['']")) 
(add-hook 'inferior-python-mode-hook 'python-reinstate-current-directory) 

我想出了一个非常简单的解决方案。 只需添加到您的.emacs下面一行:

(setenv "PYTHONPATH" "/path/to/where/modules/are") 

这将设置命名变量值的环境变量的值。现在

,打字在Emacs Python解释器...

>>> import sys 
>>> sys.path 
>>> ['/path/to/where/modules/are', '/usr/share/emacs/23.1/etc' ... ] 

...和定制import的工作。