How can I run matplotlib in Pycharm on macOS? - python

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

Related

Import packages works in idle.exe but not in IDE (python)

Please note that I am a complete beginner and installed python simply by going to the website and clicking "install". It used to work fine.
Now suddenly I have this frustrating situation where I can run just about anything in the idle.exe found in the scripts section of arcgispro, but cannot run the same .py file in an IDE. As a beginner this is obviously a headache as I would like auto code formatting, suggestions, etc. Literally any IDE would be fine (spyder, pycharm). The problem is that every single time there is some kind of error with the package imports. e.g. from pycharm:
import shapefile ModuleNotFoundError: No module named 'shapefile'
It's not just shapefile... this is happened at random times with numpy and also matplotlib.
When I look at "Installed Apps" on windows, it just says Python 3.10.0 and Python Launcher.
No, there is no file that I created called "shapefile.py"
A lot of solutions suggest things with pip... I have absolutely no idea what pip is...is it installed program? where do I find that?
EDIT: I just found out that the system paths of the two are different so this explains why one works but the other doesn't... but how can I make it so that the IDE would work?
Ok, simple answer:
In Pycharm, go to the bottom and click on Python Console. Then type pip install pyshp. Voila! Assuming it works for other packages as well. Still do not know where to make pycharm work with the other environment...

PyCharm finds library but not module

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.

Trying to install packages with pip properly

This is a very vague question but I am really stuck. I have been working with python for a little bit to try and use some of their functions in opencv (cv2) and other open source libraries. But whenever I try and use pip I will always get an error about improper installation or more commonly when trying to import the installed package idle throws an error that a line of code in the library that is causing an exception. This does not just happen to one library but nearly all of them (I have tried stuff like opencv, tensorflow, urllib). I have tried reinstalling different versions of python (multiple times) and reinstalling it but none of that seems to work. I don't know what to do but really want to be able to use these tools. I use windows 10 and use the default "User" user on admin.
Any help on possible options would be most appreciated!
This was happening to me as well.
In my case I wanted to work with Anaconda and Spyder. I was following some books and tutorials saying how to install stuff with the pip and so on, but it didn't work. What solved it was to install PyCharm and use those same pip functions directly on the PyCharm console (not the windows cmd). Then everything worked automatically and I have whatever library I installed from PyCharm available at Spyder without doing anything else.
Summary:
Get Pycharm -> link
use the same pip install directly in the PyCharm console
A-Hopefully it will work
If it doesn't: Can you elaborate on which IDE are you using? Most likely there is someone here who can help you.

Fake module for autocomplete support Pycharm [duplicate]

When writing Python code using compiled extensions (the OpenCV Python bindings, for example), PyCharm doesn't seem to be aware of their availability. The imports are marked with a grey underline, saying "unresolved reference" as a tooltip, and autocomplete doesn't work, either. (Except for the function names already used in the code.)
This isn't caused by wrong module paths, the code runs without error when started. Also, after I import the modules in a Python shell, autocomplete starts working as expected.
Is there a solution for that or is this an architectural limitation for compiled extensions? Are there any other IDEs that manage to cope with this problem?
The imports are marked with a grey underline, saying "unresolved reference" as a tooltip
This most probably means that PyCharm can't see the module you import. In editing mode, PyCharm relies on availability of Python sources of imported modules. If a module is not written in Python but is a C extension module, PyCharm generates a 'skeleton' that contains function prototypes, and uses it for completion.
In shell mode, PyCharm uses live imported objects for completion, with slightly different results.
Make sure that your OpenCV installation is visible for the Python interpreter you chose for the project (File / Settings / Python interpreter). If the interpreter is correct, try removing and re-adding it (this is time-consuming a bit, sorry).
If nothing helps, file a bug.
I have noticed a difference in pycharm behavior depending on the way to import.
using:
import cv2
the auto completion doesn't work,
while with:
from cv2 import cv2
auto completion works
I had to hardlink the binary into the folder lib-dynload of my interpreter.
$ cd /usr/lib/python3.7/lib-dynload
$ sudo ln /usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so cv2.cpython-37m-x86_64-linux-gnu.so
The paths may vary in your environment. I didn't test it on OSX or Windows, but it may work there too. The lib-dynload folder is here:
PyCharm currently does not scan compiled extensions/binaries which are in a path manually added to the interpreter in the IDE. I have filed a bug with Jetbrains in YouTrack. You might want to have a look at it and possibly the discussion I initiated in their discussion forum (link is in the bug description). I'd appreciate if you could vote for this issue to be resolved in YouTrack if you are a PyCharm user facing the same problem.
Try clicking "Reload" button in File | Settings | IDE Settings | Python interpreters. That got it working for me.
In my case on OS X 10.8 and PyCharm 3, IDE was automatically picking different installations of Python. I noticed this in Eclipse Pydev, which picked up the one right one and worked as expected. It was not easy to notice the difference between the two:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
I follow the instructions under this question:
How to install OpenCV on Windows and enable it for PyCharm without using the package manager
After that it does not work and I reinstall the pycharm ide without any other changes and now it is working perfectly.
I know that this is not the best answer, but after a lot of time wasted and trying different workarounds this was the one that solve my problem, I hope it can help you.
After two days test,I finally fix this issue:
The difference:
Uninstall python 3.7.2,install python 3.7.7.
Change the path where python install.(I strongly doubt that the cause is that my PATH of opencv-python has some Chinese characters.It should have only English).
Then do:
Install the opencv-contrib-python.
I hate to give a "works for me" answer, but maybe the details on my environment will help you identify the problem on your end.
I've never used PyCharm before, but I just did a test on Mac 10.6.6 using PyCharm 1.1.1, with Macports opencv +python26. The autocomplete worked fine for me the first time. I also closed and re-ran PyCharm and was able to autocomplete without doing anything further. I also had no issue with autocomplete for other native extensions I tried like cjson, procname.
.
Perhaps it is a platform-specific issue (Windows?), or a bug affecting an older version of PyCharm?
In my case, include opencv in the path install-opencv-4-on-windows. and add it to the project settings, if none of this works for you, I recommend that you install anaconda change the python interpreter and use the anaconda interpreter.
for this go to : file -> settings -> project:test -> python interpreter and select conda interpreter
if you dont have anaconda you can download at https://www.anaconda.com/
follow the steps in the link python-opencv to install opencv in anaconda

Unable to use downloaded third-party library from within PyCharm

I'm working on a script that will plot data onto a map using the Basemap library. I'm trying to import Shapely as well for use in this same script. I'm working with Anaconda2 for Python2.7 in a Windows 7 environment. I used conda install to download the tar.bz2 file from the Conda Packages site (using Windows command line) and it looked like it all installed correctly.
When I open Pycharm and look at my accessible site-packages, I can see this package. However, when I try to use it within my script, I get an error saying that the package does not exist. I ran the script through the debugger to see if it would shed any more light, but I got the same error. Here's a screenshot of my available site packages when I go to Settings-->Project Interpreter from within Pycharm.
Screenshot of PyCharm site packages available
Is there something special I need to do in order to access this package from within a fresh Python file? I was trying to say "import shapely" or "import osx-64-shapely", but both give me the same "package does not exist" error message. I've been able to successfully use other third-party libraries within Python, so I'm not quite sure what the error is here....
I'm new to SO - if you need more details or there's some piece of info I didn't include, please let me know. Thank you for your help!
EDIT: I am NOT asking what the difference is between conda and pip, or how to use pip within PyCharm. I have used both successfully before to install third-party libraries. What I am asking is what might cause a third-party library that appears to have installed successfully from the command line become inaccessible from within PyCharm when I attempt to import it.
I'm not familiar with Shapely but I was astonished to see the name osx-64-shapely as a site-package for your python installation which is in windows 7. Are you sure you downloaded the right file? :)

Categories