简体   繁体   English

Emacs Elpy和Web2py

[英]Emacs elpy and web2py

I have setup Python code completion by installing elpy from Melpa , and it seem to work (mostly) as advertised. 我已经通过从Melpa安装elpy来设置Python代码完成功能 ,并且它似乎按宣传的方式工作(大部分情况下)。

However I want it to also complete the web2py API, and I figured the best way to achieve this is to use the web2py shell, instead of the normal shell. 但是,我希望它也能完成web2py API,我发现实现此目的的最佳方法是使用web2py shell,而不是普通的shell。

0th try 0次尝试

Googleing for Emacs , web2py and auto-completion yielded no useful results. 谷歌搜索Emacsweb2py自动完成没有产生有用的结果。

1st try 第一次尝试

So I added to my ~/.emacs.d/init.el file 所以我添加到了~/.emacs.d/init.el文件

(setq python-shell-interpreter-args 
      "/path/to/web2py/web2py.py --plain --import_models --shell=myapp")

... did not work. ... 不工作。

2nd try 第二次尝试

Following the elpy doc I realize I probably need to customize the elpy-rpc-python-command variable. 遵循elpy doc之后,我意识到我可能需要自定义elpy-rpc-python-command变量。

So I wrote this ~/bin/rpc-web2py script such as: 所以我写了这个~/bin/rpc-web2py脚本,例如:

#!/bin/sh
# note: the $@ need to relate to python becase elpy what's to pass `-W` to it.
python2.7 "$@" /path/to/web2py/web2py.py --shell=myapp --plain --import_models

and customized the Elpy Rpc Python Command to be Other: ~/bin/rpc-web2py 并将Elpy Rpc Python命令自定义为Other: ~/bin/rpc-web2py

... still no "db." ...仍然没有“ db”。 completion. 完成。

help 救命

Am I approaching this problem in the right way? 我是否以正确的方式解决了这个问题? I am not committed to any specific code-completion solution, and am willing to have a fresh .emacs.d if need be. 我不致力于任何特定的代码完成解决方案,并且愿意在需要时使用一个新的.emacs.d

Was anybody able to have a similar working setup? 有人能有类似的工作设置吗?

One obvious oversight from my part was that I didn't add the /path/to/web2py to my $PYTHONPATH environment variable, as helpfully stated by Baris's on the code... blog. 我这方面的一个明显疏忽是,我没有将/path/to/web2py$PYTHONPATH环境变量中,正如Baris 在代码...博客上所指出的那样。

The other part of the answer comes from The Web2py book by Massimo Di Pierro, that has this to say: 答案的另一部分来自Massimo Di Pierro的《 Web2py》一书 ,内容如下:

Using general purpose IDEs with web2py 在web2py中使用通用IDE

The general problem with these IDEs (except those which support web2py) is that they do not understand the context in which models and controllers are executed and therefore autocompletion does not work out of the box. 这些IDE(支持web2py的IDE除外)的普遍问题是,它们不了解执行模型和控制器的上下文,因此自动完成无法立即使用。

To make autocompletion work the general trick consists in editing your models and controllers and adding the following code: 为了使自动完成工作有效,一般的技巧是编辑模型和控制器并添加以下代码:

if False:
    from gluon import *
    request = current.request
    response = current.response
    session = current.session
    cache = current.cache
    T = current.T

The import block does not change the logic as this is never executed but it forces the IDE to parse it and understand where the objects in the global namespace come from (the gluon module) thus making autocompletion work. import块不会更改逻辑,因为它永远不会执行,但会强制IDE对其进行解析,并了解全局名称空间中的对象来自何处(gluon模块),从而使自动完成工作成为可能。

If you are relying on variables in your models (such as database definitions) you may consider adding to the list like this: 如果您依赖模型中的变量(例如数据库定义),则可以考虑将其添加到列表中,如下所示:

from db import *

You can also consider importing all models. 您也可以考虑导入所有模型。

if False:  
    from gluon import *
    from db import *  #repeat for all models
    from menu import *

While this is more a workaround than a solution, I will take it, and yasnippet that already comes as part of elpy is the logical tool to make this workaround trivial. 尽管这不是解决方案,而是解决方案,但我将采用它,而elpy附带的yasnippet是使该解决方案变得微不足道的逻辑工具。

If somebody will come up with some less workaround-ish solution I will gladly accept that answer. 如果有人会提出一些不太可行的解决方案,我将很乐意接受该答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM