Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 457
floqqi Avatar asked Feb 21 '13 08:02

floqqi


People also ask

How do I open PyDev project in Eclipse?

Go to Window → Open Perspective → Other and choose PyDev, then click OK. If you look at the upper right corner you will see that the perspective has changed from "Java" to "PyDev".

How do I run code in PyDev?

Go to the menu: Alt + R + S + The number of the Run you wish (It can be Python, Jython, unit-test, etc). Note: if you were using unit-tests, you could use: Ctrl+F9 to run the unit-tests from the module (and even selecting which tests should be run -- and if Shift is pressed it's launched in debug mode).

Is Eclipse compatible with Python?

PyDev is a plugin that enables Eclipse to be used as a Python IDE (supporting also Jython and IronPython).


3 Answers

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__)
like image 83
Cid Avatar answered Oct 27 '22 15:10

Cid


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.

like image 26
Fenikso Avatar answered Oct 27 '22 16:10

Fenikso


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

like image 25
Eldelshell Avatar answered Oct 27 '22 16:10

Eldelshell