PyCharm finds library but not module - python

I am using PyQt5 in PyCharm Community Edition. I have installed qt5-serialport as described in this question and when I use python on the command line I am able to do from PyQt5.QtSerialPort import QSerialPort and use the QSerialPort module with no problem in the interactive python environment and when running a python script that makes use of the QSerialPort module, so it's clear that python itself is finding the module just fine. However in PyCharm, the module is not found despite the fact that I'm using the system python as the interpreter. On the line from PyQt5.QtSerialPort import QSerialPort PyCharm underlines QSerialPort in red and it doesn't offer any tooltip suggestions for methods etc. on any QSerialPort object I instantiate.
Normally when I get this kind of problem I install the package using the installer within PyCharm but this particular package doesn't seem to be in any public repo listing I can find. (Somehow yay found it, despite it not being listed in the searchable AUR). Other modules in the PyQt5 package import in PyCharm without problems. How can I get PyCharm Community Edition to recognize this module?

Installing it on the terminal should work using pip from python.org.
pip install PyQt5
This approach appears to work since it has the core features for finding, downloading, and installing packages from various Python package indexes. More info on pip from python.org.
According to the Troubleshooting tips from JetBrains listed for PyCharm, there is useful info on debugging import errors using PyQt. So this may be the cause as to why installation through the IDE was not working even though PyQt was installed on the interpreter.

Related

PyCharm error - No module named 'sip' (PyQt4)

OK, so I need to make a project in PyQt, and I'm using PyQt4 since I found more tutorials on this and it is easier for me.
I installed PyCharm as my IDE. I haven't had any python compilers so I installed Python 3.7.5 (from Microsoft Store since PyCharm recommended that), and configured the interpreter in PyCharm (so now, python works)
Then, I needed to install PyQt4, and since it didn't work from PyCharm's project interpreter -> install package, I downloaded "PyQt4-4.11.4-cp37-cp37m-win_amd64" and installed it with pip.
Then, I installed sip also with pip.
The thing is that I get an error in my code in PyCharm.
from PyQt4 import QtGui
ModuleNotFoundError: No module named 'sip'
I also configured all my Windows PATHs, when I installed sip it gave me an error saying that the path isn't added. I added that path.
I also added the pyqt4 path.
These are my paths:
C:\Users\b997a\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\sipbuild
C:\Users\b997a\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages
'C:\Users\b997a\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts
And these are all the packages I think I have installed in PyCharm:
Do you know what I can do? I really don't want to use PyQt5 since I know a found tutorials on exactly what I want to do on PyQt4 and I don't really have time to learn PyQt5 now...
I also tried to see if Python 3.7.5 Shell (IDLE) will raise an error, and yes, it did. I have the same error in Python's IDLE.
What is the problem? It worked for a while until I deleted Python a few weeks ago, but now it doesn't work anymore.
Thanks a lot!
I have recently faced such issues, so my recommendations are:
It's good to activate your virtual env for your project.
Usually pip should install the required binaries, but if it doesn't, try searching the same from pycharm -> settings and add directly from there. This has always worked for me.
P.S. I was not able to add comment, so I posted it as answer.

Modules Not Found - I cannot externally run a .py file developed using PyCharm

I am using PyCharm to develop a python project, which uses an external library called win10toast. I have installed win10toast using PyCharm. However, when I tried to run the .py file using cmd (i.e Externally running the python file), an error shows up:
ModuleNotFoundError: No module named 'win10toast'.
I have python 3.6.4. I installed win10toast using PyCharm.
from win10toast import ToastNotifier
I expect the program to run without any error, but currently I am getting the ModuleNotFound error.
Python can be tricky to run properly because it is sensitive to where you installed your dependencies (such as external libraries and packages). If you installed Python to one directory, but accidentally installed the external library to another directory, when you run your .py program, it will be unable to call from the external library because it doesn't exist in the same library that Python is running from.
Lookup where you installed Python on your computer and then find where you installed the external library. Once your find where you installed the external library, move its entire package content to the same directory where Python is installed. Or better yet, reinstall the external library with pip into the same directory as Python.
If you're on Mac, Python and its related dependencies are usually stored somewhere in /User/bin. If you're on Windows, it will be stored somewhere in your C:// directory (possibly somewhere in C:\Users\username\Local\AppData). If you're on Linux, it will be stored somewhere in /usr/bin. Whatever you do, don't move Python from wherever it is because sometimes that can mess up your system for certain operating systems like Mac, which comes with its own version of Python (Python 2.7 I believe, which is outdated anyway).
Lastly, you may have two different versions of Python on your computer, which is common; Python 2.7 and Python 3+. If you wrote your program in one version, but ran it from the other, the external library can only be called from whichever Python version you installed it to. Try running your .py program with python3 instead of python (or vice versa) and see what happens. If it works with one python version over the other, that tells you that the external library is installed in the other version's directory.
That should solve your issue.
I would suggest that you not use PyCharm to install packages, at least not
if the result deviates in the slightest from doing a "pip install" at the command line. I see no reason to involve PyCharm in configuring Python installations. It's just asking for trouble.
I admit that I'm not familiar with the practice I'm suggesting you avoid. I've been using PyCharm since pretty much the week it came out (was an avid user of the IntelliJ Python plugin before that), and have never once considered doing anything but installing Python modules at the command line. That way, I'm sure right where those modules are going (into which base Python install or venv). Also, I know I'm doing all that I can to minimize the differences that I might see between running code in PyCharm and running it at the command line. I'm making my suggestion based solely on this practice having never gone wrong for me.
I have multiple base Python versions installed, and dozens of venvs defined on top of those. PyCharm is great at allowing me to indicate which of these I want to apply to any project or Run/Debug configuration, and utilizing them seamlessly. But agin, I administer these environments at the command line exclusively.
I still experience issues in switching between the command line and PyCharm in terms of one module referencing others in a single source tree. My company has come up with a simple solution to this that insures that all of our Python scripts still run when moving away from PyCharm and its logic for maintaining the Python Path within a project. I've explained the mechanism before on S.O. I'd be happy to find that if anyone is interested.
The library win10toast installed in the directory: YOUR_PYCHARM_WORKSPACE\PycharmProjects\YOUR_PROJECT_NAME\venv\Lib\site-packages
but when you are running your program using cmd, pycharm interpreter uses site-packages directory that you installed python at there. for Ex: C:\Python27\Lib\site-packages
So, you can install the win10toast library to this windows directory using pip.

How can I run matplotlib in Pycharm on macOS?

I can run code through terminal and have a matplotlib window pop up with the graph but not through Pycharm. All I get is an error saying:
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
I'm not sure how to install Python as a framework or what that really means. I've googled about but I don't want to mess my system up.
Can someone offer a solution at least of what I should be trying to do or what to google around for.
Sorry if I lack some of the technical language/knowledge to explain this problem. Thanks
macOS High Sierra 10.13.1
If you install matplotlib with pip, or conda, you should have a directory ~/.matplotlib.
Inside that directory create, or open if it already exists, a file called matplotlibrc.
To make TkAgg the default backend just add the line backend: TkAgg
I found a short term fix until I can make changes to the necessary files from user Iron Pillow.
import matplotlib
matplotlib.use('TkAgg')
I just insert this before I import any matplotlib subpackages.
I encountered a similar situation that a graph report library based on matplotlib works fine on Conda, Jupyter, but not in Pycharm. It can generate a multi-page pdf. But when I run a program that calls the report library inside Pycharm on MacOS, and the pdf can not be generated properly.
My python 3.6 installation is from Anaconda. I found two solutions. First solution is to run pythonw from a terminal. This is obviously outside of Pycharm, and is not as convenient as you wish.
The second solution is to add "backend: TkAgg" to matplotlibrc.
This file can exist in several places. If this change is meant to be personal, then this file can live under ~/.matplotlib. You can even add this file in the current working directory, to make it part of the program you run.
This is the least intrusive solution without having to add python code that breaks platform compatibility. Formal documentation can be found in the "The matplotlibrc file" section of https://matplotlib.org/users/customizing.html
You just have to install python. You can do that using Homebrew.
In a terminal run:
sudo brew install python

Pycharm, PyQt5, and Python 2.7 -- No code completion

I've got:
Python 2.7 (Anaconda x64), which works great
PyQt5 (installed using pip install python-qt5)
PyCharm 4.04 Professional (recently upgraded from 3.4.1)
A small Qt application
After much hair-pulling (since Riverbank doesn't provide a PyQt5 binary for Python 2.7, only for 3.3+) I got everything working thanks to Marcus Otosson's pre-compiled binary packages.
Qt is now installed and fully functional. My Qt application runs great!
But the application isn't finished yet, and PyCharm won't do code completion for the PyQt modules. It won't even recognize that any PyQt5 sub-modules (like Qwidgets) exist: even though they work just fine, I still get the red squiggly "Unresolved Reference" warning.
How do I fix this? I assume this has to do with the inherent difficulties in generating skeletons for binary *.pyd files. How does it work exactly? Can I manually generate code skeletons, or import them from somewhere they generated correctly?
Uninstalling and re-installing PyCharm didn't help. Neither did re-configuring the interpreter to force the skeleton generator to run again.
Please help before I go bald.
Can you find PyQt5 from your project's External Libraries?
If you install it through pip, you should be able to see the library directly. Once the library can be referred, it will do the code-completion for you.
I am using PyCharm4(community Edititon) on Windows with PyQt4, the autocomplete is fine. As PyQt4 is actually a dynamic library to be dynamic loaded by the python interpreter ( you can look into the PyQt4 folder, there is no python files there except some init.py), auto-complete depends on the source code of PyQt4 itself, so when using pycharm, it usually generally some local python cache for complete. For my machine, a typically file looks like:
C:\Users\cui.PyCharm40\system\python_stubs-762174762\PyQt4\QtCore\QString.py
You can also try install PyQt document from Pycharm.
File->settings->Tools->python external document->PyQt
Had the same problem in PyCharm 2017.1.1. Don't do
import PyQt5.QtWidgets
do
from PyQt5 import QtWidgets

Installing Pillow for Python on Windows

I am fairly new to Python and trying to install the Pillow package on Windows 7. I downloaded and ran the MS Windows installer Pillow-2.2.1.win-amd64-py3.3.exe from here. It appeared to install fine. If I run the simple line of code:
from PIL import Image
directly from the Python interpreter, it runs fine. A help() on PIL gives me the package contents.
But when I try to run the same line of code from within a script, I get an ImportError: No module named PIL. What am I missing?
(Note that I've been able to import sys and import MySQLdb from within scripts just fine.)
Resolved: sure, enough, I'm running Python 2.7 when I run scripts. (I think I vaguely recall having to install an older version so I could interface with MySQL.) Thank you all for pointing out that I should check the version being used.
For third-party modules for Windows, my go-to resource is Christoph Gohlke's Python Extension Packages for Windows. You can find the latest version of Pillow here. Make sure you're working with the python.org version of Python.
As far as your specific error, it's hard to tell exactly without a traceback, but make sure your script is calling the correct version of Python. If you have also installed Python 2.7, for example, your script may be calling that instead of 3.3.
In such cases I'm simply printing the sys.path at the beginning of the script in trouble and comparing it with the one from the working python interpreter. In most cases I was running the script with a different python interpreter.
In my case , I was referring to wrong pip folder.
Changed virtual environment in pycharm to point to right pip folder to solve this issue
import sys
print ( sys.path )

Categories