PyCharm解决 - flask.ext.sqlalchemy vs flask_sqlalchemy [英] PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy
问题描述
如果我在应用程序中使用以下格式,除PyCharms解析/自动完成功能外,一切正常:
ext.sqlalchemy import SQLAlchemy
如果我在应用程序中使用以下格式,一切正常。但是,唉,它不是正确的方式来导入库:
from flask_sqlalchemy import SQLAlchemy
$ p
$ b有没有办法让PyCharm正确地解析第一个语法?
> 命名空间是一个转换命名空间,请参阅 Flask Extension Development 文档的/flask.pocoo.org/docs/0.10/extensiondev/#ext-import-transition\">扩展导入转换部分:
有一段时间我们推荐使用Flask扩展的命名空间包。这在实践中证明是有问题的,因为存在许多不同的竞争命名空间包系统,并且pip会在不同的系统之间自动切换,这给用户造成了很多问题。
Flask扩展应该促使用户从
flask.ext.foo
而不是flask_foo
或flaskext_foo
,以便扩展程序可以转换到新的程序包名称
因此,在转换版本之间,
flask.ext
添加了别名,当导入flask.ext时,会自动尝试导入
。但是这种转变现在是没有意义的。你不再会发现仍然依赖于flask_ [name]
[名称]flask.ext
的软件包。
因此,它是完美的来使用实际的模块名称,并使PyCharm自动完成模块内容。
只有 才能使用
flask.ext
如果您仍在使用旧版本的扩展,并且需要将来兼容。那未来已经在这里了。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?
解决方案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 offlask_foo
orflaskext_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 importflask_[name]
packages when importingflask.ext.[name]
. But that transition is now moot; you no longer will find packages that still rely solely onflask.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.这篇关于PyCharm解决 - flask.ext.sqlalchemy vs flask_sqlalchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文