标签:sheet flask eclipse make share answer data se
Viewed 14k times 12I 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?
4 Answers
ActiveOldestVotes 26The 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
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
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
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.
step2: Go to 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
after clicking OK
this will pop up:
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.
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.
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
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).
标签:sheet,flask,eclipse,make,share,answer,data,se 来源: https://www.cnblogs.com/kungfupanda/p/13494395.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。