导入模块的 Pycharm 自动完成 [英] Pycharm autocomplete for imported modules

查看:53
本文介绍了导入模块的 Pycharm 自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,并试图熟悉语法和语言.我试用了 PyCharm IDE,发现它很舒服.

I'm new to python and trying to get comfortable with the syntax and the language. I gave a shot to PyCharm IDE and found it very comfortable.

唯一的问题是自动完成并没有像我预期的那样工作,作为学习过程和研究一些模块的一部分,它对我来说非常重要.

The only problem is that auto-completion isn't working as I expected and it is very important to me as part of the learning process and looking into some modules.

我查看了有关自动完成问题的所有其他问题,但没有一个可以帮助我.

I looked at all the other questions regarding the auto-complete issue and none of them could help me.

即使没有自动完成功能,我的代码也能正常工作,但我已经习惯了,真的很想享受这个功能.

My code works even without the autocomplete but I'm very used to it and really wish to enjoy this feature.

我尝试来回更改我的项目解释器,但没有任何改变.我尝试重新启动计算机 PyCharm - 没有用.我尝试了无效缓存,确保省电模式关闭 - nada.

I tried changing my project interperter back and forth and nothing changed. I tried restarting PyCharm, the computer - didn't work. I tried Invalidate Cache, made sure the power save mode is off - nada.

还有其他想法吗?是否有负责 PyCharm 自动完成的辅助流程?如果有,它的名字是什么?问题是否可能出在我的虚拟环境中?我看到软件包已安装在其中,那么可能是什么问题?

Are there any other ideas? Is there a side process responsible for PyCharm autocomplete? If so, what is it's name? Is there a possiblity the problem is within my virtual env? I see the package is installed in it so what could possibly be the problem?

以下是 lxml 缺少自动完成的示例:

Here is an example of missing autocomplete for lxml:

这是interperter窗口:

And here is the interperter window:

请帮帮我,自动完成对我享受编程的乐趣至关重要.

Please help me out here, autocomplete is crucial for me to enjoy programming.

推荐答案

Python 是一种动态类型语言,因此函数的返回类型并不总是预先知道.PyCharm 通常会查看函数的来源来猜测它返回的内容.在这种情况下不能,因为 etree.parse 是用 Cython 编写的,而不是 Python.如果你让 PyC​​harm 去定义函数,它会给你一个存根.

Python is a dynamically typed language, so the return type of a function is not always known in advance. PyCharm generally looks at the source of a function to guess what it returns. It can't in this case because etree.parse is written in Cython, not Python. If you ask PyCharm to go to the definition of the function it gives you a stub.

Python 生态系统最近开始解决这个问题,提供了各种方法来使用类型提示注释文件,以供外部工具(包括 PyCharm)使用.一种方法是通过 .pyi 文件.在 typeshed 项目中可以找到其中的大量集合.这个问题表明为 lxml 编写提示被证明是困难的,并且不想在 typeshed 存储库中有不完整的存根,他们被移到了自己的存储库 here.存根确实非常不完整,当我尝试在 PyCharm 中下载和使用它们时,结果非常糟糕.他们正确识别 etree.parse 返回一个 etree._ElementTree,但是 _ElementTree 的存根只有两个方法.

The Python ecosystem has recently started to tackle this problem by providing various ways to annotate files with type hints for use by external tools, including PyCharm. One way is through .pyi files. A large collection of these can be found in the typeshed project. This issue shows that writing hints for lxml was proving difficult, and not wanting to have incomplete stubs in the typeshed repo, they were moved to their own repo here. The stubs are indeed very incomplete, and when I tried downloading and using them in PyCharm the results were pretty dismal. They correctly identify that etree.parse returns an etree._ElementTree, but the stub for _ElementTree only has two methods.

通过直接在 Python 文件中进行注释,我得到了更好的结果,例如

I got much better results by annotating directly in the Python file, e.g.

tree = etree.parse(path)  # type: etree._ElementTree

(你可以通过查看type(tree)来找出类型)

(you can find out the type by checking type(tree))

PyCharm 本身以某种方式知道 _ElementTree 上的方法是什么,所以现在自动完成工作.不幸的是,使用 .pyi 文件似乎让 PyC​​harm 忘记了这些知识.

PyCharm itself somehow knows what the methods on _ElementTree are so now autocomplete works. Unfortunately it seems that using .pyi files makes PyCharm forget this knowledge.

这里是关于类型提示的文档PyCharm.

Here is documentation on type hinting in PyCharm.

是的,一般来说,您将不得不习惯较少的自动完成和一般类型信息以及静态分析.幸运的是,我认为有很多其他语言无法弥补的地方:)

And yes, in general you will have to get used to less autocompletion and general type information and static analysis. Fortunately I think there is a lot to make up for it that isn't possible in other languages :)

这篇关于导入模块的 Pycharm 自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆