Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy

If I use the following format in my application, everything works, except PyCharms resolving / autocomplete feature:

from flask.ext.sqlalchemy import SQLAlchemy

If I use the following format in my application, everything works. But, alas, it is not the correct way to import the libraries:

from flask_sqlalchemy import SQLAlchemy

Is there any way to make PyCharm resolve the first syntax correctly?

like image 869
Harold Smith Avatar asked Aug 27 '14 02:08

Harold Smith


People also ask

What is difference between Flask-SQLAlchemy and SQLAlchemy?

One of which is that Flask-SQLAlchemy has its own API. This adds complexity by having its different methods for ORM queries and models separate from the SQLAlchemy API. Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult.

How to fix No module named Flask_ SQLAlchemy?

The Python "ModuleNotFoundError: No module named 'flask_sqlalchemy'" occurs when we forget to install the Flask-SQLAlchemy module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install Flask-SQLAlchemy command.

Can I use SQLAlchemy with Flask?

Flask-SQLAlchemy is a Flask extension that makes using SQLAlchemy with Flask easier, providing you tools and methods to interact with your database in your Flask applications through SQLAlchemy. In this tutorial, you'll build a small student management system that demonstrates how to use the Flask-SQLAlchemy extension.

How do I know if SQLAlchemy is installed Flask?

To check which version of sqlalchemy is installed, use pip show sqlalchemy or pip3 show sqlalchemy in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu) to obtain the output major.


3 Answers

FYI. flask.ext is deprecated, and the right way is:

from flask_sqlalchemy import SQLAlchemy

In case anyone found this SO question on Google.

like image 26
lepture Avatar answered Oct 17 '22 07:10

lepture


The flask.ext namespace is a transistion namespace, see the Extension Import Transition section of the Flask Extension Development docs:

For a while we recommended using namespace packages for Flask extensions. This turned out to be problematic in practice because many different competing namespace package systems exist and pip would automatically switch between different systems and this caused a lot of problems for users.

and

Flask extensions should urge users to import from flask.ext.foo instead of flask_foo or flaskext_foo so that extensions can transition to the new package name without affecting users.

So to transition between versions, the flask.ext alias was added, which will automatically try to import flask_[name] packages when importing flask.ext.[name]. But that transition is now moot; you no longer will find packages that still rely solely on flask.ext.

As such, it is perfectly fine to use the actual module name and have PyCharm autocomplete the module contents.

You only really have to use flask.ext if you are still using an older version of the extension and need to be future compatible. That future is already here.

like image 142
Martijn Pieters Avatar answered Oct 17 '22 07:10

Martijn Pieters


Use a virtualenv and set that virtualenv for your project in PyCharm. I had the same problem as you do and after setting the correct virtualenv (which contains the flask and flask_sqlalchemy extension) my problem is solved.

To set a virtualenv for your project in PyCharm (from JetBrains Web Help):

To add an existing virtual environment to the list of available interpreters In the Project Interpreter page of the project settings, click . In the drop-down list, choose Add local.

enter image description here

In the Select Python Interpreter dialog box that opens, choose the desired Python executable, and click OK.

Moreover try adding requirements.txt to the root of your project, afterward PyCharm will notify you to install missing dependencies which might help.

like image 32
mehdix Avatar answered Oct 17 '22 07:10

mehdix