Qt.py icon indicating copy to clipboard operation
Qt.py copied to clipboard

Intellisense/Autocomplete for PyCharm

Open ben-hearn-sb opened this issue 7 years ago • 71 comments

I cannot seem to get Qt.py to autocomplete for PyCharm. Is there any concise instructions on how to do this? Or is it not possible?

I have dug in a little and my suspicion is that because Qt.py is using Qt5 bindings and my system Python is 2.7. Could this be the case?

  • Ben

ben-hearn-sb avatar May 02 '17 13:05 ben-hearn-sb

Solutions in 2021

Hello, I'm from the future. As this thread got quite long I'll summarise the two best solutions here.

  1. Make the file Qt.py into a package Qt/__init__.py and move the .pyi files into it.
Qt/
  __init__.py
  QtCore.pyi
  QtWidgets.pyi
  ...
  1. Alternatively, install the pyi files alongside Qt.py.
Qt.py
Qt/
  QtCore.pyi
  QtWidgets.pyi
  ...


Thanks @ben-hearn-sb,

because Qt.py is using Qt5 bindings and my system Python is 2.7.

Qt 5 bindings - PySide2 and PyQt5 - work well with Python 2.7, so that shouldn't matter.

For a process to import Qt.py, there needs to be at least one binding available to it. It sounds like PyCharm may be kicking off a dedicated process in the background without access to a binding.

Does autocomplete work with a plain binding, such as PySide or PyQt4?

mottosso avatar May 02 '17 14:05 mottosso

@mottosso yes PyQt4 has always worked for me in PyCharm and so has PySide if you map it to the correct module in the maya bin folder

ben-hearn-sb avatar May 02 '17 15:05 ben-hearn-sb

Try doing exactly that for Qt.py as well, it'll need a native binding in order for you, or PyCharm, to import it.

mottosso avatar May 02 '17 15:05 mottosso

I've had users ask for this in vscode as well. Would be awesome to have.

I'm guessing pyi files would be the solution, as mentioned by @chadrik here. However, I've never dabbled with that so I don't know much about it.

fredrikaverpil avatar May 03 '17 08:05 fredrikaverpil

Yep, pyi files would solve this in PyCharm.

Regarding vscode, pythonVSCode is by far the most popular python plugin, and it uses jedi for python inspection. Jedi is used for inspection in a lot of editors (vim, sublime, textmate, kate, gedit, ...) and already supports PEP484 type annotations. Support for pyi files is in progress.

Some tools are still catching up with PEP484, but it will definitely be the standard that everyone rallies around in the next year or so.

chadrik avatar May 04 '17 01:05 chadrik

Here's what I get in Sublime at the moment.

untitled project

I suppose you guys are talking about something more involved, like help on argument signatures, and completion on submodules too.

mottosso avatar May 04 '17 07:05 mottosso

How have you set that up for Sublime?. (personal opinion but I depise sublime for Python coding Pycharm all the way)

If the setup for sublime can be replicated for PyCharm then it is golden!

On 4 May 2017 at 09:49, Marcus Ottosson [email protected] wrote:

Here's what I get in Sublime at the moment.

[image: untitled project] https://cloud.githubusercontent.com/assets/2152766/25694409/7720bd2c-30a6-11e7-9512-ccf225faa6ba.gif

I suppose you guys are talking about something more involved, like help on argument signatures, and completion on submodules too.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-299118153, or mute the thread https://github.com/notifications/unsubscribe-auth/ANDpHiDl95Wa_TiaMimojykiWyFhuK2Iks5r2YL9gaJpZM4NOJ-u .

ben-hearn-sb avatar May 04 '17 09:05 ben-hearn-sb

No, there wasn't any set-up. I suspect the editor runs a dir on the module in the background, potentially followed by an inspect to get argument signatures and things, and hand it to you in the form of auto-completion.

If your editor isn't doing this already, then perhaps there is a plug-in for it. Python alone would likely be unable to provide help on argument signatures for Qt binaries though, but I'd expect it to work with pure Python libraries.

(personal opinion but I depise sublime for Python coding Pycharm all the way)

No comparisons here please, nothing but love and butterflies.

mottosso avatar May 04 '17 09:05 mottosso

I was thinking of generating some stubs for Qt.py but not sure what the legality of distributing stubs is. I assume as long as its open source, it follows Qt's licensing.

dgovil avatar May 08 '17 23:05 dgovil

What made you think of legalities with stubs? Would they contain copyrighted material? Looking at these it appears to be just plain import statements.

mottosso avatar May 09 '17 06:05 mottosso

Here we go.

  • https://github.com/Ultimaker/Uranium/blob/master/stubs/PyQt5/QtWidgets.pyi

That's likely more difficult to distribute (GPL licensed), but we should (in theory) be able to use these as-is, though preferable the PySide2 equivalent, as all members of Qt.py come from there.

mottosso avatar May 09 '17 06:05 mottosso

@mottosso I'm not sure if function signatures are covered under licenses. But my understanding from searching online is that it shouldn't be an issue.

dgovil avatar May 10 '17 02:05 dgovil

No comparisons anymore I swear :)

Do you have any suggestions of what to take a look at to get intellisense working for PyCharm?

  • Ben

On 10 May 2017 at 04:00, Dhruv Govil [email protected] wrote:

@mottosso https://github.com/mottosso I'm not sure if function signatures are covered under licenses. But my understanding from searching online is that it shouldn't be an issue.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-300352357, or mute the thread https://github.com/notifications/unsubscribe-auth/ANDpHvVfNkqR4DTAqgP7ctHLG-H0Y7Ehks5r4Ro3gaJpZM4NOJ-u .

ben-hearn-sb avatar May 10 '17 08:05 ben-hearn-sb

I think that Qt.py's all-in-one-module approach might make this task more difficult. Qt.py dynamically generates modules, but static type checkers like pycharm and mypy rely on static definitions. I'm pretty sure that the stubs need actual module counterparts. I know it's a big-ish change but it might be worth breaking out the single module into a package.

chadrik avatar May 10 '17 22:05 chadrik

If the stubs are separate files, it wouldn't make a difference in how Qt.py is structured though. For example a lot of the stubs structures don't match 1:1 the actual packages.

As long as PyCharm sees the stubs dir before the actual lib dir, it would see that as the correct autocompletion.

Does anyone know if its possible to drop in custom pyi files into PyCharms skeletons folder? I believe it gets cleared on every update...

dgovil avatar May 10 '17 23:05 dgovil

If the stubs are separate files, it wouldn't make a difference in how Qt.py is structured though. For example a lot of the stubs structures don't match 1:1 the actual packages.

@dgovil, thanks for clarifying that. That said, in IMHO the most convenient way to distribute and use pyi files is to place them next to their .py counterparts, which would require making a roughly matching package structure.

The alternative is to put them on the PYTHONPATH before their py counterparts, which could become a drain on import time on network drives if many packages were setup this way. It would be really handy if they supported a separate search path like MYPYPATH.

chadrik avatar May 11 '17 00:05 chadrik

I just made a quick attempt to generate pyi files using stubgen.py: https://drive.google.com/drive/u/0/folders/0B0b1IUnYqENgQTY2WW1xX29fekU

I was using PyQt5 as the binding, so not sure how this will work unless you use that - but I would assume PyQt5-generated files should work even if your underlying binding is something else, although it would probably be preferable to generate such files from PySide2. I'm also using 1.0.0b3 of Qt.py.

I'm using Visual Studio Code and I'm actually not sure how to load these pyi files in there (if yet possible). But anyone on e.g. PyCharm could perhaps take a look?

If these actually work, it seems quite easy to create such files but for PySide2 instead, which perhaps would be enough?

fredrikaverpil avatar May 11 '17 05:05 fredrikaverpil

I now see they differ quite a lot from the QtWidgets.pyi one @mottosso posted.

They probably won't work then. I wonder why they ended up this way...

Even when I create stubs from PyQt5 directly, they look this sparse in comparison with this.

fredrikaverpil avatar May 11 '17 05:05 fredrikaverpil

I made a little workaround for PyCharm. dull import in Qt.py: if 2 * 2 == 5: from PySide import QtGui, QtCore QtWidgets = QtGui

and then when i import modules from from Qt.py like this: "from Qt import QtWidgets as QtGui" PyCharm looks for PySide QtGui and autocomplete all my Widgets.

listyque avatar May 16 '17 12:05 listyque

Killer workaround man. Totally works on our end also!

On 16 May 2017 at 14:06, listyque [email protected] wrote:

I made a little workaround for PyCharm. dull import in Qt.py: if 2 * 2 == 5: from PySide import QtGui, QtCore QtWidgets = QtGui

and then when i import modules from from Qt.py like this: "from Qt import QtWidgets as QtGui" PyCharm looks for PySide QtGui and autocomplete all my Widgets.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-301761339, or mute the thread https://github.com/notifications/unsubscribe-auth/ANDpHptg17mIfbOuFYIyG6yhbe7XN9Ssks5r6ZEugaJpZM4NOJ-u .

ben-hearn-sb avatar May 24 '17 15:05 ben-hearn-sb

Cool! I wonder if we can incorporate this into Qt.py somehow.. any ideas?

mottosso avatar May 24 '17 15:05 mottosso

You could put this inside qt.py

If False: from PySide2 import QtWidgets, QtCore, QtGui

And then have that for all 4 bindings in a row. PyCharm should try and find it for one of them.

On Wed, May 24, 2017, 8:34 AM Marcus Ottosson [email protected] wrote:

Cool! I wonder if we can incorporate this into Qt.py somehow.. any ideas?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303762033, or mute the thread https://github.com/notifications/unsubscribe-auth/AEPL0irqpIChCNmEO0yarsu9Uiw_K5ZLks5r9E4IgaJpZM4NOJ-u .

dgovil avatar May 24 '17 15:05 dgovil

We have overriden PySide2 and PySide in Maya so if PySide was hardcoded it would not work for us.

Perhaps just a try-catch for import PySide/PyQt4?

On 24 May 2017 at 17:44, Dhruv Govil [email protected] wrote:

You could put this inside qt.py

If False: from PySide2 import QtWidgets, QtCore, QtGui

And then have that for all 4 bindings in a row. PyCharm should try and find it for one of them.

On Wed, May 24, 2017, 8:34 AM Marcus Ottosson [email protected] wrote:

Cool! I wonder if we can incorporate this into Qt.py somehow.. any ideas?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303762033, or mute the thread <https://github.com/notifications/unsubscribe-auth/ AEPL0irqpIChCNmEO0yarsu9Uiw_K5ZLks5r9E4IgaJpZM4NOJ-u> .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303765184, or mute the thread https://github.com/notifications/unsubscribe-auth/ANDpHkasbN4R3yHnOjmxOAtA-PaG1Hd3ks5r9FBvgaJpZM4NOJ-u .

ben-hearn-sb avatar May 24 '17 16:05 ben-hearn-sb

Since it's behind an if false, it would never execute. It's just that pycharm has a naive finder for imports by looking for the word import followed by the package and module names.

On Wed, May 24, 2017, 9:02 AM Ben Hearn [email protected] wrote:

We have overriden PySide2 and PySide in Maya so if PySide was hardcoded it would not work for us.

Perhaps just a try-catch for import PySide/PyQt4?

On 24 May 2017 at 17:44, Dhruv Govil [email protected] wrote:

You could put this inside qt.py

If False: from PySide2 import QtWidgets, QtCore, QtGui

And then have that for all 4 bindings in a row. PyCharm should try and find it for one of them.

On Wed, May 24, 2017, 8:34 AM Marcus Ottosson [email protected] wrote:

Cool! I wonder if we can incorporate this into Qt.py somehow.. any ideas?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303762033, or mute the thread <https://github.com/notifications/unsubscribe-auth/ AEPL0irqpIChCNmEO0yarsu9Uiw_K5ZLks5r9E4IgaJpZM4NOJ-u> .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303765184, or mute the thread < https://github.com/notifications/unsubscribe-auth/ANDpHkasbN4R3yHnOjmxOAtA-PaG1Hd3ks5r9FBvgaJpZM4NOJ-u

.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mottosso/Qt.py/issues/199#issuecomment-303770533, or mute the thread https://github.com/notifications/unsubscribe-auth/AEPL0gXe0fxAQ4Wr7bC5DPWCsa8gVbuhks5r9FSIgaJpZM4NOJ-u .

dgovil avatar May 24 '17 16:05 dgovil

https://github.com/mottosso/Qt.py/pull/205

Autocomplete working in PyCharm with no effect on the working of the library itself.

dgovil avatar May 24 '17 18:05 dgovil

If False: from PySide2 import QtWidgets, QtCore, QtGui

I just tried this in vscode (with pylint), and that didn't work :(

fredrikaverpil avatar May 24 '17 19:05 fredrikaverpil

If we were to make it into documentation, how could it best be documented?

Alternatively, is there another mechanism in which we could get this into PyCharm, similar perhaps to the Package Control of Sublime, if we distribute an individual "patch" of sorts just for PyCharm.

mottosso avatar May 24 '17 20:05 mottosso

The easiest cross IDE way would probably be to distribute a modified version of the PyQt5 stubs and people can just add it to their Python Path or autocomplete path. Each editor has its own system of supporting autocomplete. PyCharms is just the most advanced (IMHO anyway)

Shipping extensions for PyCharm is a pain. It requires writing Java plugins for the editor. If you wanted to go the extension route, you'd have to make a new extension per IDE since I don't think there's a common system.

dgovil avatar May 24 '17 20:05 dgovil

he easiest cross IDE way would probably be to distribute a modified version of the PyQt5 stubs

Cool, could we try that? I can set it up so that it ends up on pip install and is available as pip install Qt.pyAutoComplete or the like.

mottosso avatar May 25 '17 05:05 mottosso

The easiest cross IDE way would probably be to distribute a modified version of the PyQt5 stubs and people can just add it to their Python Path or autocomplete path.

'pip install Qt.pyAutoComplete' or the like.

That's a really good idea. I like how it is then separated from Qt.py. If you set up a repo, I'd be happy to help out any way I can.

fredrikaverpil avatar May 25 '17 07:05 fredrikaverpil