Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...