PyQt4 names showing as undefined in eclipse, but it runs fine - python

I am using Eclipse 3.7.1 with the latest PyDev add-in for Python coding. I am using PyQt4. At the top of my file I have:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
In addition, I have the PyQt4 tree included in the Project Explorer listing. However, eclipse still thinks the names like QMainWindow are undefined. The code runs fine. How may I get eclipse to recognize those names.
Thanks

PyQt is actually a wrapping of C++ Qt libraries. So they are not .py files and PyDev can't analyze them to get what is in them. You need to add PyQt4 in the Forced Builtins tab, so that PyDev can use a Python shell to "look into" those libraries and know what is in them. That will also give you code-completion for PyQt.
Apart from that, it is usually not a good practice to use from foo import *. You'll be importing everything inside your namespace and you wouldn't know which is coming from where. Moreover you might have name clashes that mask each other. Though it is unlikely with PyQt, still I'd suggest you get used to from PyQt4 import QtGui, QtCore and reference classes like QtGui.QMainWindow.

it happens sometimes that PyDev lose its mind...
If restarting Eclipse don't do the trick, consider doing this:
Click on the "Apply" button and select your python interpreter. That should force Eclipse to recover existing libraries.

Related

Missing classes from qgis.core module only in PyCharm's editor

I would like to use PyCharm Community Edition 2020.1.2 x64 on Windows 10 as IDE for writing QGIS plugins. I need to base on old version - QGIS 2.18, so I need python 2.7, qt4, and also core modules from QGIS 2.18. I downloaded QGIS 2.18 from official site. It provides a ready to use python environment as .bat script, that sets all needed python paths. So I used path D:\Program Files\QGIS 2.18\bin\python-qgis-ltr.bat to set main python interpreter for my project.
After that I was able to run python console in PyCharm, import all QGIS modules, and any classes from QGIS standard collection like QgsFeature, QgsVectorLayer etc. Everything was fine untill I tried to import this classes from PyCharm editor in .py file as from qgis.core import QgsFeature. In editor PyCharm sees the package qgis.core and I'm able to import it, but PyCharm's code completion inside the module sees only classes that are saved in D:\Program Files\QGIS 2.18\apps\qgis-ltr\python\qgis\core\__init__.py e.g. QgsEditError class, but all standard QGIS class underlines on red.
Python console ran on exactly the same interpreter see the same sources as are visible in "External Libraries" tab in PyCharm, so command:
qgis.core.__path__ returns ['D:\\Program Files\\QGIS 2.18\\apps\\qgis-ltr\\python\\qgis\\core'],
qgis.core.__file__ returns 'D:\\Program Files\\QGIS 2.18\\apps\\qgis-ltr\\python\\qgis\\core\\__init__.pyc,
dir(qgis.core) returns full list of QGIS standard core classes.
If I run the .py script, in which PyCharm's code validator is showing incorrect import object by red underline, the script is executed without any error and I can easly print the imported QgsFeature object. Why my PyCharm's editor see different content of qgis.core module then console ran also from IDE on exactly the same interpreter?
Allow me to qualify myself. I lost interest for adventures and trail blazing in computing some time ago. I won some big battles and lost some too. And I unnecessary wasted a lot of time that could have been better spent. Nowadays, I just wait a reasonable period of time before upgrading anything, and wait a spell before downloading the newest anything, and pretty much avoid trying to do things outside a conventional path.
So your post got me curious and I looked around some. I found very little information about PyCharm being used building QGIS plugins. That pretty much tells me all I want to know: that it's not real common for PyCharm to be used in building QGIS plugins.
But there was quite abit of information about building QGIS plugins with python. There's even a QGIS Tutorial & Tips on the subject. QGIS suggests using the Qt Creator and further goes on to say that QGIS itself is written using the QT framework. "For plugin development, we use an application called Qt Creator."
If you overcome your current predicament I'd like to hear how you accomplished it.
For what it's worth, here's the link to the QGIS Tutorials & Tips.
https://www.qgistutorials.com/en/docs/building_a_python_plugin.html
Finally, the problem solved itself. It was something wrong with building a binary skeletons of C++ libraries, what PyCharm does every time the application is launched. My PyCharm didn't want to do it and I found many similar problems with other libraries (e.g. Qt) that people were struggling with. After two days my PyCharm just fixed itself and created all needed binary skeletons for my QGIS libraries.

Eclipse, PyDev, PySide, importing, and parsing

I am working in Eclipse but running my code in Nuke. Nuke uses PySide, but it's built into Nuke so Eclipse can't parse it. I cannot install PySide in my environment (VFX studio rules) but I do have PyQt4. So I can change my import from PySide to PyQt4 and it parses correctly in Eclipse - but it will error when loading Nuke. So what I need is a way to get Eclipse/PyDev to use PyQt4 while Nuke uses PySide. I thought perhaps I could use PyDev's String Substitution scheme but either it doesn't do what I want or I'm not using it right. Any help is greatly appreciated!
Den
Use Rob's qtshim.py.
You'll find it here: https://github.com/rgalanakis/practicalmayapython/blob/master/src/chapter5/qtshim.py
Import QtCore, QtGui etc. From qtshim instead of PySide or PyQt. Code written with this will work for both environments.
I use my own version of something like this to work with PyQt and PySide.
Hope that was useful.

Configure PyCharm IDE to organize the imports the way Eclipse does (Django development)

I am using PyCharm for Django web application development. The tool is great, except that I need to add every import statement manually (i.e. the 'optimize imports' feature doesn't work).
Is there any way to make it work the same way Eclipse does when developing Java applications?
You don't need to type imports manually, there is a relevant documentation page, see:
Creating Imports
Imagine you've written the following code forgetting to import os:
print os.getcwd()
Then, focus the word os and hit Alt+Enter combination, you'll get the popup from which you can choose what to import, choose os from the list.
Also, there is an "Import Assistant" that fires up tooltips on unresolved imports on the fly.
And, FYI, "Optimize Imports" feature works with existing imports and removes unused entries.
Hope this is what you were asking about.

Installation of PyQt4

I currently begin to use Python (2.7) with Eclipse(on windows). To run my script,I would need PyQt4 but I meet some problems when I try to install it.
I downloaded
PyQt4-4.10.3-gpl-Py2.7-Qt4.8.5-x64.exe,
PyQt-win-gpl-4.10.3.zip
and sip-4.15.2.zip
from the following link: http://www.riverbankcomputing.co.uk/software/pyqt/download .
I launched the execution of the first feature and a folder PyQt4 appeared into C:\Python27\Lib\site-packages.
To my mind, PyQt4 installation was finished and I simply did:
Eclipse>Preferences>Pydev>Interpreter-Python>python27>Apply
Nevertheless, when I try to import any function of PyQt4, I am said that the module name is unknown.
Could you help me to solve this problem?
Thank you very much
I want to comment for this .... but my reputation doesn't allow me to :(
anyway You could try this answer ..... (link below)
which is something like that :
Copy from This Answer
PyQt is actually a wrapping of C++ Qt libraries. So they are not .py
files an
d PyDev can't analyze them to
get what is in them. You need to add PyQt4 in the Forced Builtins tab,
so that PyDev can use a Python shell to "look into" those libraries
and know what is in them. That will also give you code-completion for
PyQt.
Apart from that, it is usually not a good practice to use from foo
import *. You'll be importing everything inside your namespace and you
wouldn't know which is coming from where. Moreover you might have name
clashes that mask each other. Though it is unlikely with PyQt, still
I'd suggest you get used to from PyQt4 import QtGui, QtCore and
reference classes like QtGui.QMainWindow.

How do I make PyCharm/PyDev/other IDEs offer code completion for IronPython namespaces?

I like PyCharm and have used it before for my Python projects, but I just started messing with IronPython and I can't figure out how to make PyCharm or any other IDE for that matter (except for VS which works fine) recognize the .NET libraries.
For example, I have the code:
from System.Environment import *
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
The above code works fine, but the "System" part is underlined grey everywhere with the message "Unresolved reference System". The IronPython documentation explains that System is not a module but rather a namespace. If I do type(system) I get <type 'namespace#'>. So is there a way to make PyCharm/PyDev recognize the namespaces as well? On a side note, the PATH is fine, everything is fine.
For performance reasons, PyCharm does not generate Python stubs for .NET assemblies by default. You can trigger the generation by putting the caret on an unresolved reference in an import statement, pressing Alt-Enter and selecting the "Generate stubs for binary module ..." quickfix.
Just to note, this should work properly in PyDev (just make sure that you configure an IronPython interpreter and configure your project as an IronPython project).
Another note is that the code above should actually be:
from System import Environment
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Not PyCharm but Michael Foord has info on how to do this w/ Wing: http://www.voidspace.org.uk/ironpython/wing-how-to.shtml - PyCharm may have some similar mechanism available.

Categories