PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy

时光怂恿深爱的人放手 提交于 2019-11-28 21:14:11

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.

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

from flask_sqlalchemy import SQLAlchemy

In case anyone found this SO question on Google.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!