如何让 eclipse/pydev 乐于在 Windows 上看到 Flask 扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14997336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make eclipse/pydev happy to see flask extensions on windows?
提问by floqqi
I stumbled upon this articleand followed all steps. But pyDev won't see my flask extensions and that's really annoying. There's only one thing (and I think this is the key):
我偶然发现了这篇文章并遵循了所有步骤。但是 pyDev 不会看到我的烧瓶扩展,这真的很烦人。只有一件事(我认为这是关键):
Touch /site-packages/flaskext/__init__.py
Touch /site-packages/flaskext/__init__.py
Touch
is a unix util I think. Is there an equivalent to this on Windows?
Touch
我认为是一个unix util。在 Windows 上有类似的东西吗?
回答by Fenikso
The Eclipse uses static analysis of modules by default. flask.ext
builds import list dynamically. To force dynamic analysis using Python shell add flask.ext
to forced builtins list.
Eclipse 默认使用模块的静态分析。flask.ext
动态构建导入列表。要使用 Python shell 强制进行动态分析,请添加flask.ext
到强制内置列表中。
Go to Preferences -> PyDev -> Interpreters -> Python Interpreter
. Select your interpreter, go to Forced Builtins
tab. Click New...
and enter flask.ext
.
去Preferences -> PyDev -> Interpreters -> Python Interpreter
。选择您的口译员,转到Forced Builtins
选项卡。单击New...
并输入flask.ext
。
This requires PyDev to forcefully analyze module through a shell.
这需要 PyDev 通过 shell 强制分析模块。
For more details see PyDev manual.
有关更多详细信息,请参阅PyDev 手册。
回答by Eldelshell
I'm also struggling with this and the problem seems to be in the way that Flask imports the extensions. If you open the flask/ext/__init__.py
file you'll see it uses importer. I don't think PyDev likes this much, so I've edited this file with the fixed imports:
我也在为此苦苦挣扎,问题似乎出在 Flask 导入扩展的方式上。如果您打开该flask/ext/__init__.py
文件,您会看到它使用了导入程序。我不认为 PyDev 喜欢这么多,所以我用固定导入编辑了这个文件:
import flask_login as login
import flask_sqlalchemy as sqlalchemy
import flask_wtf as wtf
def setup():
from ..exthook import ExtensionImporter
importer = ExtensionImporter(['flask_%s', 'flaskext.%s'], __name__)
importer.install()
setup()
del setup
I've also found that Flask-SQLAlchemy imports broke too, so instead of doing db.Column
as explained in the documentation, directly use sqlalchemy import, i.e. from sqlalchemy import Column, ForeignKey
我还发现 Flask-SQLAlchemy 导入也坏了,所以不要db.Column
按照文档中的解释去做,直接使用 sqlalchemy 导入,即from sqlalchemy import Column, ForeignKey
回答by gandalf
If you have your project in a virtual environment and you want add the project in eclipse so that the project uses libraries that are installed on the virtual environment, then you should follow the following steps.
如果您的项目位于虚拟环境中,并且希望在 eclipse 中添加该项目,以便该项目使用安装在虚拟环境中的库,那么您应该按照以下步骤操作。
step 1:
let's say the absolute path to your virtual environment is:
C:\Users\sadegh\Desktop\flask_eclipse\fe\venv
第 1 步:假设您的虚拟环境的绝对路径是:
C:\Users\sadegh\Desktop\flask_eclipse\fe\venv
go to window->preferences->PyDev->interpretors->Python Interpretor
in the Scripts
directory, there is python.exe
which is the python interpreter that has been assigned to this virtual environment. This executable will be the new python interpreter that we will add to eclipse.
去window->preferences->PyDev->interpretors->Python Interpretor
在Scripts
目录中,有python.exe
哪些是已经被分配给该虚拟环境中的Python解释器。这个可执行文件将是我们将添加到 eclipse 的新 python 解释器。
step2:
Go to window->preferences->PyDev->Interpreters->Python Interpreter
第2步:转到 window->preferences->PyDev->Interpreters->Python Interpreter
In the right pane you will see this:
click on new button then this window will pop up:
write anything you want in the Interpreter Name
field and write the absolute path of the python.exe file that was mentioned in step 1 in the Interpreter Executable
field
你想在写什么Interpreter Name
领域,并写在步骤1中提到的python.exe文件的绝对路径Interpreter Executable
场
after clicking OK
this will pop up:
select all the items then click OK
选择所有项目然后单击 OK
step3: select the newly added interpreter in the above pane, then in the below pane go to Forced Builtin
tab and click on new button on right hand side of this below pane.
步骤3:在上面的窗格中选择新添加的解释器,然后在下面的窗格中转到Forced Builtin
选项卡并单击下面窗格右侧的新按钮。
and in the window that pops up write flask.ext
.
并在弹出的窗口中写入flask.ext
。
step4: everything is set now:
step4:现在一切都设置好了:
if you want to start a new project:
when you are creating a new PyDev Project
select the new Interpreter that we created as the Interpreter of this project.
如果你想开始一个新项目:当你创建一个新项目时,PyDev Project
选择我们创建的新解释器作为这个项目的解释器。
if you want to convert an existing project to a flask project on your virtual environment right click on project and go to properties and in PyDev-Interpreter/Grammar
change the Interpreter to the new interpreter that we have created.
如果要将现有项目转换为虚拟环境中的烧瓶项目,请右键单击项目并转到属性并将PyDev-Interpreter/Grammar
解释器更改为我们创建的新解释器。
note: If you want the eclipse to run the server for you in the virtual environment you can run the server from within the code that contains the Flask() instance like this:
注意:如果您希望 eclipse 在虚拟环境中为您运行服务器,您可以从包含 Flask() 实例的代码中运行服务器,如下所示:
if __name__ == '__main__': #here i assume you have put this code in a file that
app.run() #contains variable "app", which contains the instance of #Flask(__main__)
回答by nneonneo
touch
will create a blank file if it doesn't exist, or update the file's modification time if it does exist.
touch
如果不存在则创建一个空白文件,如果存在则更新文件的修改时间。
For this purpose, echo > /site-packages/flashext/__init__.py
at a command-line should suffice. (The file won't be blank, but only contains a single newline which is semantically equivalent for Python).
为此,echo > /site-packages/flashext/__init__.py
在命令行中就足够了。(该文件不会是空白的,而只包含一个换行符,这在语义上与 Python 相同)。