Python模式导入问题

标签 python emacs

我正在尝试将 Emacs 用作 python 编辑器,当我只评估(C-c C-c)单个文件时它工作正常,但是当我评估一个在同一目录中导入另一个文件的文件时,我收到一个错误,说文件无法导入。

有人知道解决方法吗?

提前致谢

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

错误是,

  ImportError: No module named hlutils 

最佳答案

我认为问题在于 Emacs 的 python 模式运行 Python 的方式。如果我输入 M-x run-python,那么我会看到:

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

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

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

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

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

没有评论,以及以下有用的变更日志条目:

2008-08-24  Romain Francoise  <romain@orebokech.com>

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

关于Python模式导入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2658475/

相关文章:

python - 传递给变量时排除字典的特定键

python - 合并音频和视频ffmpeg-python时出错

python - 构建docker镜像时无法安装sklearn

python - 如何减少 PIL 中的 png 图像文件大小

python - 计算X小时后的时间?

python - emacs 中 python 代码折叠的范围

git - 如何使用 magit 的 git mv?

emacs - elisp 代码破坏了缓冲区,而不是保存在其他地方......为什么?

Emacs:如何将 "M-x"提示更改为其他内容?

java - IntelliJ IDEA 有类似emacs 的增量搜索吗?