我正在尝试使用Emacs作为python编辑器,当我评估(Cc Cc)只有单个文件时它工作正常但是当我评估一个文件导入同一目录中的另一个文件时,我得到一个错误,说该文件不能进口.
有没有人知道一个解决方法?
提前致谢
编辑:顺便说一句,我在Ubuntu机器上使用Emacs23.
错误是,
ImportError: No module named hlutils
Run Code Online (Sandbox Code Playgroud)
Jim*_*ndy 16
我认为问题在于Emacs的python-mode运行Python的方式.如果我输入M-x run-python,那么我看到了这个:
>>> import sys
>>> '' in sys.path
False
>>>
Run Code Online (Sandbox Code Playgroud)
而如果我从shell运行python解释器,我看到:
>>> import sys
>>> '' in sys.path
True
>>>
Run Code Online (Sandbox Code Playgroud)
这似乎是由于run-pythonprogmodes/python.el中的以下代码:
(let* ((cmdlist
(append (python-args-to-list cmd)
'("-i" "-c" "import sys; sys.path.remove('')")))
Run Code Online (Sandbox Code Playgroud)
没有评论,以及以下有用的ChangeLog条目:
2008-08-24 Romain Francoise <romain@orebokech.com>
* progmodes/python.el (run-python): Remove '' from sys.path.
Run Code Online (Sandbox Code Playgroud)
我想说这是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)
Run Code Online (Sandbox Code Playgroud)
use*_*215 12
由于发布了此前的答案,因此可以使用一个选项来更改默认行为(从路径中删除当前目录)以在路径中包含cwd.
这么简单
(setq python-remove-cwd-from-path nil)
Run Code Online (Sandbox Code Playgroud)
你的.emacs应该解决这个问题.
打开终端并输入emacs -q. 这将启动 emacs,而不加载初始化文件 ( .emacs)。是否会出现同样的行为?如果没有,则意味着您的 .emacs 文件存在问题。
编辑:
当您在 Emacs 的 Python 模式中使用 Cc Cc 运行脚本时,包含该脚本的目录不会添加到sys.path. 我通过放置发现了这一点
import sys
print sys.path
Run Code Online (Sandbox Code Playgroud)
在测试脚本的顶部。这就是为什么 Python 找不到驻留在同一目录中的其他模块的原因。
当您从命令行运行脚本时,Python 会将包含脚本的目录添加到sys.path. 这就是为什么相同的脚本可以在命令行中运行的原因。
您可以通过编辑 ~/.bashrc 文件并添加来解决此问题
export PYTHONPATH=/path/to/Python/scripts
Run Code Online (Sandbox Code Playgroud)
保存 ~/.bashrc 后,关闭 emacs 并再次启动,以确保对 .bashrc 的更改生效。
| 归档时间: |
|
| 查看次数: |
5523 次 |
| 最近记录: |