How to make eclipse/pydev happy to see flask extensions on windows?

Viewed 14k times 12

I stumbled upon this article and 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):

Touch /site-packages/flaskext/__init__.py

Touch is a unix util I think. Is there an equivalent to this on Windows?

     share  improve this question    edited Feb 21 '13 at 8:13     asked Feb 21 '13 at 8:07 floqqi 1,00722 gold badges99 silver badges1919 bronze badges add a comment

4 Answers

ActiveOldestVotes 26  

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.

Go to Preferences -> PyDev -> Interpreters -> Python Interpreter. Select your interpreter, go to Forced Builtins tab. Click New... and enter flask.ext.

This requires PyDev to forcefully analyze module through a shell.

For more details see PyDev manual.

share  improve this answer    answered Feb 14 '14 at 15:39 Fenikso 8,18433 gold badges3838 silver badges6868 bronze badges
  •   This looks very promising. In the mean time I switched to PyCharm, so I can't (and do not want to) test this on my own. However, I will mark this question as solved, since you're referencing to the official documentation. – floqqi Feb 14 '14 at 22:27
  • 3 This fixes Unresolved Imports for any of the flask.ext modules I've installed. Adding the Flask modules to the Python PATH did NOT fix the Unresolved Imports. – Jonathon Faust Jan 19 '15 at 19:05
  •   This looks promising, but it did not work for me in LiClipse 3.6.0 on Mac OS X 10.11. Possibly a bug in PyDev as bundled in LiClipse? – Jim DeLaHunt May 16 '17 at 23:50
  •   @JimDeLaHunt I do not use PyDev anymore, so I have no idea. – Fenikso May 17 '17 at 10:40
add a comment 2

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:

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

share  improve this answer    edited May 16 '13 at 11:22     answered May 16 '13 at 10:42 Eldelshell 5,30377 gold badges3838 silver badges5959 bronze badges add a comment 1

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.

step 1: let's say the absolute path to your virtual environment is: C:\Users\sadegh\Desktop\flask_eclipse\fe\venv

The content of venv folder

go to window->preferences->PyDev->interpretors->Python Interpretor in the Scripts directory, there is python.exe 

enter image description here

 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.

step2: Go to window->preferences->PyDev->Interpreters->Python Interpreter 

enter image description here

In the right pane you will see this: 

enter image description here

click on new button then this window will pop up: 

enter image description here

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 Executablefield

after clicking OK this will pop up: 

enter image description here

select all the items then click 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.

enter image description here

and in the window that pops up write flask.ext.

step4: everything is set now:

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. 

enter image description here

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.

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:

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__)
share  improve this answer    answered Feb 11 '17 at 19:41

gandalf 1,87944 gold badges2222 silver badges2626 bronze badges
  •   This will add the interpreter globally for all projects, even though the interpreter in question would be a project-specific interpreter. Is there a way to add the interpreter so it is only project-specific? – FluxIX Mar 29 '17 at 16:26
add a comment 0

touch will create a blank file if it doesn't exist, or update the file's modification time if it does exist.

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

share  improve this answer     

原文地址:https://www.cnblogs.com/kungfupanda/p/13494395.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Jinja2:是Python的Web项目中被广泛应用的模板引擎,是由Python实现的模板语言,Jinja2 的作者也是 Flask 的作者。他的设计思想来源于Django的模板引擎,并扩展了其语法和一系列强大的功能,其是Flask内置的模板语言。
Fullcalendar日历使用,包括视图选择、事件插入、编辑事件、事件状态更改、事件添加和删除、事件拖动调整,自定义头部,加入el-popover显示图片、图片预览、添加附件链接等,支持手机显示。
监听QQ消息并不需要我们写代码,因为市面上已经有很多开源QQ机器人框架,在这里我们使用go-cqhttp官方文档:go-cqhttp如果您感兴趣的话,可以阅读一下官方文档,如果不想看,直接看我的文章即可。
【Flask框架】—— 视图和URL总结
python+web+flask轻量级框架的实战小项目。登录功能,后续功能可自行丰富。
有了这个就可以配置可信IP,关键是不需要企业认证,个人信息就可以做。
本专栏是对Flask官方文档中个人博客搭建进行的归纳总结,与官方文档结合事半功倍。 本人经验,学习一门语言或框架时,请首先阅读官方文档。学习完毕后,再看其他相关文章(如本系列文章),才是正确的学习道路。
本专栏是对Flask官方文档中个人博客搭建进行的归纳总结,与官方文档结合事半功倍。基础薄弱的同学请戳Flask官方文档教程 本人经验,学习一门语言或框架时,请首先阅读官方文档。学习完毕后,再看其他相关文章(如本系列文章),才是正确的学习道路。 如果python都完全不熟悉,一定不要着急学习框架,请首先学习python官方文档,一步一个脚印。要不然从入门到放弃是大概率事件。 Python 官方文档教程
快到年末了 相信大家都在忙着处理年末数据 刚好有一个是对超市的商品库存进行分析的学员案例 真的非常简单~
一个简易的问答系统就这样完成了,当然,这个项目还可以进一步完善,比如 将数据存入Elasticsearch,通过它先进行初步的检索,然后再通过这个系统,当然我们也可以用其他的架构实现。如果你对这系统还有其他的疑问,也可以再下面进行留言!!!
#模版继承和页面之间的调用@app.route("/bl")def bl(): return render_template("file_2.html")主ht
#form表达提交@app.route("/data",methods=['GET','POST']) #methods 让当前路由支持GET 和
#form表达提交@app.route("/data",methods=['GET','POST']) #methods 让当前路由支持GET 和
#session 使用app.secret_key = "dsada12212132dsad1232113"app.config['PERMANENT_SESSION_LI
#文件上传@app.route("/file",methods=['GET','POST'])def file(): if request.meth
#跳转操作:redirect@app.route("/red")def red(): return redirect("/login")
#session 使用app.secret_key = "dsada12212132dsad1232113"app.config['PERMANENT_SESSION_LI
@app.route("/req",methods=['GET','POST'])def req(): print(request.headers)
#模版继承和页面之间的调用@app.route("/bl")def bl(): return render_template("file_2.html")主ht
#文件操作:send_file,支持图片 视频 mp3 文本等@app.route("/img")def img(): return send_file("1.jpg&q