mottosso / Qt.py

Minimal Python 2 & 3 shim around all Qt bindings - PySide, PySide2, PyQt4 and PyQt5.
MIT License
910 stars 254 forks source link

Intellisense/Autocomplete for PyCharm #199

Open ben-hearn-sb opened 7 years ago

ben-hearn-sb commented 7 years ago

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?

mottosso commented 7 years ago

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?

ben-hearn-sb commented 7 years ago

@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

mottosso commented 7 years ago

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

fredrikaverpil commented 7 years ago

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.

chadrik commented 7 years ago

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.

mottosso commented 7 years ago

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.

ben-hearn-sb commented 7 years ago

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 notifications@github.com 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 .

mottosso commented 7 years ago

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.

dgovil commented 7 years ago

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.

mottosso commented 7 years ago

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 commented 7 years ago

Here we go.

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.

dgovil commented 7 years ago

@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.

ben-hearn-sb commented 7 years ago

No comparisons anymore I swear :)

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

On 10 May 2017 at 04:00, Dhruv Govil notifications@github.com 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 .

chadrik commented 7 years ago

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.

dgovil commented 7 years ago

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...

chadrik commented 7 years ago

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.

fredrikaverpil commented 7 years ago

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 commented 7 years ago

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.

listyque commented 7 years ago

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.

ben-hearn-sb commented 7 years ago

Killer workaround man. Totally works on our end also!

On 16 May 2017 at 14:06, listyque notifications@github.com 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 .

mottosso commented 7 years ago

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

dgovil commented 7 years ago

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 notifications@github.com 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 .

ben-hearn-sb commented 7 years ago

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 notifications@github.com 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 notifications@github.com 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 .

dgovil commented 7 years ago

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 notifications@github.com 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 notifications@github.com 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 notifications@github.com 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 commented 7 years ago

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

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

fredrikaverpil commented 7 years ago

If False: from PySide2 import QtWidgets, QtCore, QtGui

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

mottosso commented 7 years ago

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.

dgovil commented 7 years ago

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.

mottosso commented 7 years ago

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.

fredrikaverpil commented 7 years ago

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.

mottosso commented 7 years ago

It could be this repo, I can just distribute the same repo twice with different contents.

Just put up a folder stubs/ or the like.

fredrikaverpil commented 7 years ago

I just snagged the stubs from the Uranium project and performed some changes: https://github.com/fredrikaverpil/Qt.py/tree/stubs/stubs

I don't have the time today but if anyone wishes to, please go ahead and test these.

fredrikaverpil commented 7 years ago

I can get these stubs to work with PyCharm but (unfortunately) I cannot make them work with Visual Studio Code. I'm guessing it comes down to that JEDI lacks support for pyi files.

screen shot 2017-05-29 at 06 49 02

~~All it takes is this addition to Qt.py: sys.path.append(os.path.join(os.path.dirname(file), 'stubs', 'Qt')~~

EDIT:

I was mistaken... adding to sys.path did not work for me.

Instead, I dropped a folder called "Qt" (with the .pyi contents) next to my test.py file in order to make this work.

screen shot 2017-05-29 at 06 49 22

Not entirely sure how to make this load via e.g. a sys.path.append statement inside of Qt.py, if even possible (?).

fredrikaverpil commented 7 years ago

By the way, the PyQt5 stub files were provided by Riverbank: https://github.com/Ultimaker/Uranium/issues/253

I checked with @ctismer over at PySide/pyside2 on native PySide2 stubs, and it seems they may be on their way ...as he's looking into how Riverbank generated the PyQt5 files (using SIP, it seems).

dgovil commented 7 years ago

That looks awesome.

I'm not a fan of modifying the system path for end users because it modifies the runtime pathing.

You can add system paths to the editor paths which modify the pathing for the editor itself without modifying the runtime pathing

VS Code has a user setting for autocomplete paths. It takes a list of paths. I imagine users would add the stubs path there like they would for Maya cmds autocomplete. I think it does support pyi files if it's in the autocomplete pathing rather than the Python path but I will try tomorrow.

It's not automatic but it falls in line with the process for other third party stubs systems it seems. It also means companies can set up one global directory for stubs files much like a global typeshed repo.

On Sun, May 28, 2017, 9:58 PM Fredrik Averpil notifications@github.com wrote:

I can get these stubs to work with PyCharm but (unfortunately) I cannot make them work with Visual Studio Code. I'm guessing it comes down to that JEDI lacks support for pyi files https://github.com/davidhalter/jedi/issues/839.

[image: screen shot 2017-05-29 at 06 49 02] https://cloud.githubusercontent.com/assets/994357/26536979/d3dbd094-443b-11e7-933a-47f9a2a045be.png

All it takes is this addition to Qt.py:

sys.path.append(os.path.join(os.path.dirname(file), 'stubs', 'Qt')

— 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-304572574, or mute the thread https://github.com/notifications/unsubscribe-auth/AEPL0n1RZ3Lo4k19RoCmT0r9rhH_dDraks5r-lB0gaJpZM4NOJ-u .

fredrikaverpil commented 7 years ago

@dgovil I tried using the python.autoComplete.extraPaths setting in vscode without success.

// List of paths to libraries and the like that need to be imported by auto complete engine. E.g. when using Google App SDK, the paths are not in system path, hence need to be added into this list.

"python.autoComplete.extraPaths": [],

kerekb commented 7 years ago

Any working solutions for this problem? Tried some of the comments with no luck :( Thanks in advance.

melMass commented 7 years ago

@kerekb Using the provided stubs (I got the ones from the stubs branch of @fredrikaverpil's Qt.py repo ) works in PyCharm. Unfortunately, the python add-on of VSCode do not seem to recognise these.

monkeez commented 7 years ago

Hey @mottosso are you using any python related packages in Sublime? I'm not getting any of that autocomplete behaviour for Qt.py unfortunately.

mottosso commented 7 years ago

@monkeez Unfortunately I'm not using any autocomplete myself, I'm relying on the community with need and experience for this particular issue.

KelSolaar commented 6 years ago

Hello,

I wanted to know what was the latest on that if any?

I tried @fredrikaverpil successfully with PyCharm 2017.3.3, just needed to add the stubs directory from his fork to Python path:

image

Cheers,

Thomas

fredrikaverpil commented 6 years ago

No news that I'm aware of, I'm afraid.

You could try adding a task or bug in the PySide2 issue tracker: https://bugreports.qt.io/projects/PYSIDE/issues/

mikebourbeauart commented 6 years ago

I don't quite follow how to use the stubs. I have the repo cloned, but need help finding where to place the stubs within my project and how to tell the IDE where to find them. I was using VS2015, but wouldn't mind info for PyCharm or VSCode

KelSolaar commented 6 years ago

@mikebourbeauart : With PyCharm you just have to add them to PYTHONPATH as per https://github.com/mottosso/Qt.py/issues/199#issuecomment-363987264

mikebourbeauart commented 6 years ago

@KelSolaar Ahhhh I see now. I was getting confused about whether or not to add that to the system env vars, Here's how I did it for PyCharm:

image

fredrikaverpil commented 6 years ago

I know this is not an IDE, but you can use auto-completion (ctrl + space) of Qt.py in Maya's script editor.

vlc_2018-05-02_09-04-17

herronelou commented 6 years ago

I'm using the stubs from @fredrikaverpil in PyCharm and they mostly work, the only thing is that they do not recognize Signals.

I have added: class Signal(pyqtSignal): ...in QtCore

and things like: clicked = ... # type: 'Signal' for a bunch of signals in QtWidgets, though I'm not sure that is the proper way to do it.

fredrikaverpil commented 5 years ago

It's possible that this jedi development could make stubs work in vscode!

EDIT: And I just learned that PySide2 is now shipping with stubs.

fredrikaverpil commented 5 years ago

Okay, it seems that starting with PySide2-5.12.2, all you need is PyCharm and a venv with PySide2 in it:

Screenshot 2019-05-03 at 14 42 14

MaximeEngel commented 5 years ago

Okay, it seems that starting with PySide2-5.12.2, all you need is PyCharm and a venv with PySide2 in it:

Screenshot 2019-05-03 at 14 42 14

With a pycharm updated today, and the last qt.py and PySide2-5.12.3 version it does not works for me. I search stubs in the PySide packages but i do not see them. Where are they located for yourself ?