PyCharm and PyQt5 unresolved reference but working - python

I have installed PyQt5 using the windows installer. I have tried creating some windows and stuff and it works great. Still, pycharm seems to not like PyQt5 very much, and is marking errors everywhere in my code.
As I said, PyQt5 is working great, but it's kind of annoying to have those errors which, by the way, i can not alt + enter + ignore them.
I guess it's just a matter of adding references or something to pycharm, but I have no idea how to do it and I have already spent 1 hour trying to look for solutions in stack-overflow, and none of them work. I have also tried re-installing it.
I am using windows 7 64 bits, python 3.4, and PyQt5 of 32bits because 64 didn't work for some reason.

I had similar problem because I had both PyQt4 and PyQt5 installed.
In general this can cause all sorts of problem, so the spyder-ide guys made the qtpy package, which gives an abstraction on top of the PyQt5/PyQt4/PySide.
Nowdays the qtpy is a standard library in the Anaconda distribution, and you can call it instead of PyQt5/PySide likes this:
from qtpy.QtCore import QFile, QFileInfo
from qtpy.QtGui import QIcon, QKeySequence, QPixmap
It solved me this specific problem in PyCharm.

I also have both 32 bit and 64 bit Python v3.5 and PyQt5 versions installed. That should not matter because I installed the 32 bit PyQt5 package in the 32 bit PI and the 64 bit PyQt5 package in the 64 bit PI. I installed the PyQt5 packages from Riverbanks's PyQt5 web site. Christopher Gohlke does not have them yet on his Windows extensions site, nor does PyCharm's PI dialog even list PyQt5 as an available package.
PyCharm does indeed incorrectly flag numerous warnings whether I choose the 32 bit PI or the 64 bit PI for the PyQt5 project I am working on. This appears to be a cosmetic bug only for PyQt5 code, since the code does run correctly, even if I run the code from PyCharm's Terminal window. Also, note that PyQt5 is not listed in PyCharm's PI settings dialog, even though Windows 10 File Explorer does indeed list the proper packages in the proper site-packages folder. Doing a PyCharm Invalidate/Restart does not help.
That said, you do not have to, nor should you, suppress warnings for everything. Just suppress warnings on a case by case basis only for PyCharm's incorrect PyQt5 warnings. You can do it like this:
def __init__(self):
# noinspection PyArgumentList
super().__init__()

In my case I needed in my PyCharm to go File -> Settings -> Project: MyProject -> Project structure and chose correct Project Interpreter

Another probable and easy means is to:
open the command prompt as an administrator
type in 'pip' (without the quotes)
type in: pip show PyQt5 (this shows you where PyQt5 has been installed.
follow the trace given to you from the command prompt. Now, open the folder 'site-packages' and copy every folder termed' PyQt5' to the 'lib' folder instead (ignoring those with '.. - info' is inconsequential).
In Pycharm or Visual studios, the red underline goes off and the problem of 'unresolved reference' is eliminated.

Related

python.. 64bit to 32bit and module problem

So, i developed a small python program with my machine.. I use some libraries like PyQt5 xlsxwriter.. Now, i get my exe with pyinstaller, but i discovered that users machine have 32bit system.. With vscode i easily change the python interpreter to rebuild with py 3.8 32 bit, but in this way, i have a error with the module import: Both PyQt5 and xlsxwriter are impossible to found.. I try to unistall e install it again, but never. I certainly wrong somthing but my basket of ideas isover..
Sorry for my English
Good day evryone!

ImportError: DLL load failed while importing QtCore, QtGUI

I just upgraded my Python to 3.8.3 and pyside2 to 5.15.0 and now it's showing me:
from PySide2 import QtCore, QtGui, QtWidgets
ImportError: DLL load failed while importing QtCore: The specified module could not be found.
My Environment variables are in the path and I have the latest version of both Python and PySide2...
Why is this happening??
I tried reinstalling PySide2 but nothing happens...
I think you failed to install it. Probably it collides something...
I updated PySide6.2.4 → 6.3.0 today. I run into this error again.
In my case, QtWidgets...
On windows, we usually have Local files and Roaming files.
My installation leads my files into Local files, but I don't know why, Roaming file also has python file. And the file has site-packages , naturally Pyside6.2.4 was in it. So I removed the python file in Roaming.
And then, I reinstalled PySide6. Installation itself is no problem, error didn't occur. (Because Local files already have python file which has site-packages containing Pyside6... installation had no effect.)
But I checked the installed folder, the number of files seemed to be less than usual... there are no characters like QtWidgets...
In spite of reinstalling again, the condition never changed.
I read commandline message carefully, I found out I installed other files at the same time. PySide6_Addons_6.3.0.dist-info, PySide6_Essentials-6.3.0.dist-info PySide6-6.3.0.dist-info.
Because these files are not removed in the previous handling, installation seemed to be in vain.
So I deleted PySide6, PySide6_Addons_6.3.0.dist-info, PySide6_Essentials-6.3.0.dist-info PySide6-6.3.0.dist-info all.
After that, I reinstalled PySide6.3.0. My installation sucessed.
In general, if we downloaded a python, we would download from python.org
You can download it into any drive.
If you download python, python automatically makes a Lib module at the same directory.
And then, you must set a pass for them.
I don't make sure which directory to add, it will be necessary procedure.
In my environment system path environment as follows:
Scripts module will be created for mainly command-script.
For example, they will have pyside2-rcc.exe or pyside2-uic.exe, and pip.exe.
I was warned when I made Qt-resource file because of the lack of path.
If you can't do pip, you may be able to do the same thing by writing py -m command in front of the pip command.
Moreover, if you installed PySide2, you should install into the site-packages belonging to the same directory of python.exe.
You go into the directory python.exe exists with command-prompt, you command pip the PySide2 module.(from .whl will be better)
After that, site-packages module of the Lib module will get the PySide2 packages.
For the test, I recommend you boot Python IDE and write "from PySide2 import QtCore, QtWidgets, QtGui and other codes.
Even if you follow my introduction, you may have the same problem.In that case, it is possible to be a time for transition of some other environments.
For example, AFAIK, PySide6.0 couldn't use QtWebEngine .But with PySide6.2 makes it.
I ran into the same problem when I was using Spyder with Anaconda, and I couldn't find out what is the reason. I don't make sure whether QtWebEngine can be used with Spyder with PySide6.2 even now because Anaconda can't use PySide6.x yet.AFAIK, PySide5.15 couldn't use QtWebEngine.
At any rate, I think we should know the basic, minimal environment for using python & pyside.
Anaconda is very useful, but I had no exact knowledge about controling directories for python, I was very confused.Sometimes, there may be also the problem at the spider side. (In addition to it, Anaconda recommends conda, but I often want to use pip.)I couldn't distinguish where the problem was.It makes me more confused.I have been trying to solve this problem in only the Anaconda environment.
For that reason, I think this basic installation will be a good mark.If we can execute python with this basic environment, the problem is in your
package controller.Otherwise, we cannot help waiting for the time to solve it.
I also ran into this problem.
I couldn't execute my code as it is.
When I update my Anaconda3 for python3.7.x to python3.8.3., and update my PySide2,
this error hannpened.
I have followed this solution:
https://bugreports.qt.io/browse/PYSIDE-1323
Thanks to this, I could unexpectedly do 'from PySide2 import QtCore, QtGui, QtWidgets' on the Anaconda Prompt, console.
Nonetheless, I have the same error on the spyder, IPython.
I couldn't understand why it is. I couldn't execute codes from Spyder, but I can execute from Anaconda Prompt...(It seems to me they are the same thing, on the surface at least.)
So, as my final way, I changed the current console to an external console.
It is just the same to be executed from Console Application attached by Windows.
(>python module_name.py)
I could execute my file by this way... (But unvisible errors came!)
Until this bug is repaired, I execute my file by an external console.

PyQt5 gets "No module named 'PyQt4'" error calling matplotlib.pyplot in Python 3.5

I am using Anaconda with Python 3.5.2, Matplotlib 2.0.2, PyQt5.6 on a windows 10 machine. When I import matplotlib.pyplot as plt I get the following error:
...
File "C:...\Anaconda3\Lib\site-packages\matplotlib\backends\qt_compat.py",
line 137, in <module> from PyQt4 import QtCore, QtGui
ImportError: No module named 'PyQt4'
I don't know why it would want to import from PyQt4 when it has never been installed on my machine.
This question has the same error, but on a machine that actually has PyQt4 installed.
I have checked my matplotlibrc file, I've used matplotlib.use('qt5agg') in the program, also matplotlib.rcParams['backend'] = "Qt5Agg". I've uninstalled and reinstalled all the above packages to no avail. I have even attempted to install PyQt4 just to get things going. I'm completely stumped. None of the various possible causes or remedies that I've been able to find on either SO or github have helped.
I just had the same error (even though on Windows 7). For me the problem was that there was an old matplotlibrc file in C:\Users\<username>\.matplotlib\matplotlibrc which overwrote the settings from my environment's matplotlibrc file. Deleting that file solved the issue for me.
It seems the API version is PyQt5 but the default is PyQt4,just open Tools -> Preferences -> IPython console -> Graphics -> backend,change QT4 to QT5
Looking at the file and line number in your error it looks that you have the same issue that I answered here.
That is, I think your issue is caused by having a QT_API environment variable that still is set to pyqt4 (or pyside).
I ran into a similar thing and found that the problem was having a 64-bit Anaconda installation that couldn't load the PyPt5 DLLs that were perhaps 32-bit. The short answer is that I uninstalled Anaconda 64-bit and installed the 32-bit version instead.
To work through this, I stepped through qt_compat.py where the error originates. For most of the time through this module, it’s trying to work with PyQt5, as that’s what it finds in the environment. However, when it gets to the lines below, the import fails so it tries to fall back to PyQt4, which isn’t installed with Anaconda, and thus issues the error.
if QT_API == QT_API_PYQT5:
try:
from PyQt5 import QtCore, QtGui, QtWidgets
_getSaveFileName = QtWidgets.QFileDialog.getSaveFileName
except ImportError:
# fell through, tried PyQt5, failed fall back to PyQt4
QT_API = rcParams['backend.qt4']
QT_RC_MAJOR_VERSION = 4
Testing the import statement outside of the file gave the message “DLL load failed: %1 is not a valid Win32 application.” That's what suggests the DLL mismatch, and also answers why it was trying to fall back to PyQt4.
3 steps can solve this problem:
pip uninstall pyqt5
pip uninstall matplotlib
pip install matplotlib
I had faced the same problem and here is how I fixed it.
Look into your matplotlib configuration file which is often located at
path-to/site-packages/matplotlib/mpl-data/matplotlibrc
or
path-to/Anaconda3/envs/your_env_name/Lib/site-packages/matplotlib/mpl-data/matplotlibrc
See an example of matplotlibrc here
https://github.com/daler/matplotlibrc/blob/master/rc/default
If you have pyqt5.x.x installed and have the statement 'backend : Qt5Agg' in matplotlibrc, then to use '%matplotlib qt', change '#backend.qt4 : PyQt4' to '#backend.qt4 : PyQt5'.
For an Anaconda environment, reactivate the environment.
Note if you update matplotlib in the future, your matplotlibrc will automatically be overwritten.

Python: cannot import from PyQt5.QtCore (attempting example script)

I'm introducing myself to PyQt5 through one of its included examples. On this windows 7 machine, I have installed Python 3.4 x64, and PyQt5 using its binary provided on riverbankcomputiong.com. The documentation says that the binary already includes everything necessary to run. I (perhaps incorrectly) assumed that i can safely skip the "configure" and "build" steps at Riverbank's installation guide, since the guide only talks about .zip, .tar, etc. files.
I used the tutorial located here:
http://www.pythonschool.net/pyqt/introduction-to-pyqt/
Which also says "just run the binary to install pyqt4 there is no step three."
When i attempt to run any tutorial containing reference to PyQt4 or PyQt5:
from PyQt5.QtCore import *
I get the following error message:
ImportError: DLL load failed: The specified module could not be found.
But when i enter the following:
import PyQt5
The interpreter seems to be okay with it -- no errors.
I can't help but think I've done something wrong installation, because even when I run the examples included with PyQt4/PyQt5, i get importerrors. It seems as though QtCore doesn't even exist in relation to PyQt4 or PyQt5. What's going on here?
There seem to be a few possibilities:
You might need to update your PATH environment variable to include the location where the library is installed (i.e. directory location where the DLLs are)
Take note of where you're launching the interpreter or scripts from as their current path may not necessarily include what you expect.
You can check this with:
>>> import os
>>> os.environ['PATH'].split(os.pathsep)
Make sure the installation process was truly successful (i.e. zero errors) and that you downloaded the correct binary package for your PC's architecture.
I ran into a similar issue with PySide where import PySide would work but import PySide.QtCore would fail.
I'd expect the code to work after verifying those things:
>>> from PyQt5.QtCore import *
>>>
If it doesn't, update the question with additional details like things you checked, their values, error messages, etc.

No autocomplete for PyQt4 in PyCharm

On my office PC I'm using
Python 3.4
PyQt4 Version 4.11.1
PyCharm Community Eddition 3.4.1
I installed everything in the order as stated above.
However, code completion does not work. It works for imports, but not for classes and methods.
I made a small sample program:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setWindowTitle("Test")
window.resize(300, 200)
window.show()
sys.exit(app.exec_())
In the code above none of the classes or methods were suggested by PyCharm.
If I type window.setW I would expect it to suggest setWindowTitle() but it doesn't.
Strange thing is, that I'm using the same setup on my computer at home and there it works like a (Py)charm... Autocompletion, auto imports, etc. As far as I remember I installed it the same way as I did on my office PC.
I already looked at some similiar questions but this question has no answers and others don't contain relevant information that helped me so far.
Don't know if it is relevant but at this line
from PyQt4 import QtGui
PyCharm tells me
Unresolved reference 'QtGui'
Maybe the reason for this is the same.
I had the same problem on Linux, pyqt5 and pycharm CE.
The solution was to rebuild the skeletons with:
File > Invalidate Caches / Restart > Invalidate and Restart.
The problem was solved by installing PyCharm first and then Python and PyQt.
Afterwards I had to configure the interpreter in PyCharm and it worked.
When I posted the question I installed Python, then PyQt and then PyCharm.
Oddly enough, it worked in this order on another PC.
I went through the same issue recently. Unfortunately, the install order didn't solve it for me.
There is a bug report here. Downloading the Early Access Program release worked and can be found here

Categories

Resources