PyCharm autocomplete for imported modules - python

I'm new to Python and trying to get comfortable with the syntax and the language. I gave PyCharm a shot 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.
My code works even without the autocomplete but I'm very used to it and really wish to enjoy this feature.
I tried changing my project interpreter 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.
Here is an example of missing autocomplete for lxml:
And here is the interpreter window:

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.
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.
I got much better results by annotating directly in the Python file, e.g.
tree = etree.parse(path) # type: etree._ElementTree
(you can find out the type by checking type(tree))
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.
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 :)

Install KITE, its a super fast auto suggest engine for python. It works for Pycharm,Sublime etc...
For more details view this youtube video

Related

Seems my pylance having issue with autocomplete third party module's method

I am trying to switch the editor from Pycharm to Vscode somehow.
But some aspect of autocomplete isn't working in vscode I guess.
Specifically, when I want to utilize autocomplete dealing with BeautifulSoup module,
and want to see autocomplete in deeper property,
autocomplete works properly in Pycharm, where as vscode,
shows nothing.
I roughly know it may be a problem with Pylance extrapaths something
but I could'n figure out how.
I tried some features in setting.json like adding python language server to pylance,
or some kinda of pyright plugin hoping more delicate function but didn't get it.
Can I get some direct info about this matter?
The same problem exists on github and has been closed.
Beautiful soup needs to add type stubs for this to work.
You could suggest developer updating the package on bs4.

Is it possible to change the way PyCharm displays the OpenCV documentation?

PyCharm (PY-193.6911.25, macOS 10.15.4) doesn't correctly display the OpenCV documentation.
More concretely, this is the result.
As you can see, it's a mess. Is it possible to fix this? I installed OpenCV with pip.
After googling a lot I ended up thinking there is no easy solution but... there actually is!
What i did is:
Push CTRL Q
Scroll down to the trippledot and click open source.
Push CTRL+R(replace)
replace '. ' to ''
Now docstring looks like this:
Definately not perfect, but i think it looks a bit better.
Additionally i added this as external documentation:
https://opencv-python-tutroals.readthedocs.io/en/latest/search.html?q={element.name}&check_keywords=yes&area=default
So it brings up a cv2 'tutorial'...
In PyCharm 2020.1.1 professional edition with Python 3.7, the following warning is displayed for the cv2 docstring
You need configured Python 2 SDK to render Epydoc docstrings
Epydoc is a tool for generating API documentation for Python modules, based on their docstrings.
Pycharm can't render Epydoc docstrings
The suitable interpreter is looked up among those configured in the IDE (it has to be Python 2 since Epydoc package itself hasn't been ported to Python 3 yet). In other words, you need to set up it once so that it showed up on the list at Settings | Project: ... | Project Interpreter | Show All... -- it doesn't need to be actually used in any project. Is it true in your case?
The problem is that it's not really Epytext. OpenCV uses Doxygen for documentation that we don't support and also confuse with Epytext because of the similarity of tags. There is an open issue about it.
Doxygen is confused with Epydoc and displayed as free text (not preformatted)
I'm afraid this particular feature of PyCharm is not really extensible at the moment. Besides, displaying of documentation, injecting references to symbols inside it, completion of available tags and generation of documentation stubs -- everything is tied very closely to the fact that in Python code documentation is normally provided via designated string literals, not comments. The highest level API you can plug into to provide custom documentation source for Quick Documentation popup/tool window is com.intellij.lang.documentation.DocumentationProvider if you register yours before the default one with ID pythonDocumentationProvider. But it will cover only rendering, everything else will need to be written from scratch.
The official resolution is we don't support that

how to debug cython using cygdb?

It is supposedly possible to debug a Python3/Cython project using gdb, after building gdb from source if you configure it with python2.7 as specified in the Cython debugging docu.
However, the example in the docu:
is sometimes vague (e.g. the code should be built with python3 and debugger run with python2, but I discovered python-gdb is actually linked to python2 ... so how does that work?)
is incomplete (some steps covered in code blocks, others vaguely referred to in text)
is inconsistent (e.g. references to both source.pyx and myfile.pyx)
Furthermore, it:
does not take the use of virtual environments into account
seems to assume the main function resides in the .pyx (but mine resides in a regular main.py)
does not specify what to do when your files live in different directories (e.g. like my main.py and cythonCode.pyx do)
Could someone please explain (preferably with working example) how to do debug a Python3/Cython project in a situation involving all 3 points just mentioned?
At the moment it seems I can actually get DDD to work following this Cython wiki article, but I then discovered that is the 'old' way of doing it, and it refers to the current debugging docu I also linked to. At this point however, it is unclear to me how the 'new' method functions (the old makes more sense to me) and it surely seems more complex to get it to work.

PyCharm code style check via command line

Is it possible to run the PyCharm linter / code style checks from command line and get the warnings/errors?
Extension to that: Is it possible to integrate that in my Travis tests?
To put some of the comments and some further research into an answer:
PyCharm comes with a small command-line utility bin/inspect.sh, which is documented here. This tool is quite limited though, and has some problems, e.g. it cannot run while the PyCharm IDE is running, and it reports somewhat incorrectly / different than the IDE. Related code can be seen e.g. here.
PyCharm does not do PEP8 code style checks by itself but uses the (bundled) pycodestyle tool.
Maybe these shortcomings can be fixed upstream. See e.g. this report, or this, or this.
I'm using this in Travis and GitHub Actions now, via this script.
This also does the necessary setup of a project.
This also compensates for the inspect.sh missing warnings by also using the pycodestyle tool and thus exactly matching the PyCharm IDE warnings.
Alternative, I'm thinking about writing an extended simple utility which basically does this. All the relevant PyCharm code is open source. I created a project page pychar-inspect for this. But this is just in the planing phase currently, and maybe obsolete when this will be addressed upstream.

Has anyone found a good set of python plugins for vim -- specifically module completion?

I'm looking for a suite of plugins that can help me finally switch over to vim full-time.
Right now I'm using Komodo with some good success, but their vim bindings have enough little errors that I'm tired of it.
What I do love in Komodo, though, is the code completion. So, here's what I'm looking for (ordered by importance).
Code completion, meaning: the ability to code complete modules/functions/etc. in any module that's on the pythonpath, not just system modules. Bonus points for showing docstrings when completing.
Jump-to a class definition. I'm guessing CTAGS will do this, so how do you all manage automatically updating your tags files?
Project type management for managing buffers: ideally the ability to grep for a filename in a directory structure to open it. Bonus for showing an index of class definitions while a buffer is open.
Bzr integration. Not super important, since most of it I can just drop to the shell to do.
Here you can find some info about this.
It covers code completion, having a list of classes and functions in open files. I haven't got around to do a full configuration for vim, since I don't use Python primarily, but I have the same interests in transforming vim in a better Python IDE.
Edit: The original site is down, so found it saved on the web archive.
And I write another plugin: https://github.com/klen/python-mode
Old (now its more powerful) screencast here: https://www.youtube.com/watch?v=67OZNp9Z0CQ
Old question, but I typed all this up for a misread question...
General plugin recommendations: LookupFile and a plugin for your source control system (I like Git and Git-Vim).
Python plugin recommendations: If you're using Linux, I'd recommend ipython and ipy.py (a better interactive interpreter). Improved syntax highlighting, snippets, pydoc, and for refactoring support bicyclerepairman. I got started with this post.
You may want to try looking through someone's vimfiles. Mine are on github.
For refactoring: ropevim
Here is some info on Bazaar integration if you're interested:
https://launchpad.net/bzr-vim-commands
I use pydoc.vim (I actually wrote it) a lot, try it and tell me what you think. Another one that I think is quite useful is the updated syntax file with all it's extensions that you can enable, which you can find here.
I use Pydiction (http://www.vim.org/scripts/script.php?script_id=850) it's a plugin for vim that lets you Tab-complete python modules/methods/attributes/keywords, including 3rd party stuff like Pygame, wxPython, Twisted, and literally everything. It works more accurately than other things i've tried and it doesn't even require that python support be compiled into your Vim.
Code completion: PySmell looks promising. It's work-in-progress, but alredy useful.
I personally thinkJedi Vim is the best, but it is incompatible with python-mode.

Categories

Resources