How to use Python Matplotlib with OSx and virtualenv? - python

I just started learning Python's libraries for data analysis (Numpy, Pandas and Matplotlib) but have already stumbled across my first problem - getting the Matplotlib to work with virtualenv.
When working with Python I always create a new virtual environment, get my desired Python version and libraries/dependancies into this environment. This time, the course required that I use Python3, so I installed it via HomeBrew.
The challenge:
Matplotlib needs to interact with OS
to make this happen it needs the framework build of Python (the system one)
...which is not possible if it's inside of a virtualenv, which makes it use the virtualenv build of Python
The workaround that Is supposed to be common is described at this link but I am unsure how to use this (the OSX section).
My understanding of the solution:
get Python version that I wish to use, install it system wide, NOT in a virtualenv
create a virtualenv, get dependencies that I need, this creates the virtualenv Python build
somehow trick the system into using virtualenv dependancies with system build of Python
this is done with the shell script(?) which seems to modify certain variables in shell/terminal config file(s)
Questions:
am I correct with the above "explanation to myself"?
what is the correct way to do this? from within the virtualenv, from outside of it...?
after this is done, how do I execute my Python scripts? with my virtualenv activated or not?
Many thanks!

If you are using Python 2.x then, use these commands in virtual environment:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
This makes the matplotlib work in the virtual environment too.
I used this steps to make the matplotlib running in the virtual environment.

Related

Matplotlib crashes when importing pyplot in Blender

I know this question has been asked many times before but I cannot find a solution that works for me. I'm running Python 3.5.3 under Blender 2.79 in Windows 10. As I require Blender 2.79, upgrading the Python version is not possible.
I've installed matplotlib and I can import it. However, when I try to import pyplot (e.g. import matplotlib.pyplot as plt), I get a crash to desktop with no error messages of any kind. This happens if I run Python externally or from inside Blender. The weird thing is that I was able to avoid this by changing backends to agg (matplotlib.use('agg')) and this is still working in a previous installation (so I know it's possible to get this to work !), but not in a separate, new installation of Blender. I've tried other backends but they make no difference.
The exact procedure I've tried is as follows :
Downloaded Blender 2.79 from a zip file (I'm using Windows 10) and unpacked it (call this directory /Blender/)
In /Blender/2.79/python/bin I run the command ./python -m ensurepip
In /Blender/2.79/python/scripts I run pip3 install --upgrade pip --user. This gives me pip3 version 20.2.4
In the same directory I run pip3 install --target="/Blender/2.79/python/lib/site-packages" matplotlib --upgrade. The "upgrade" switch is to prevent warnings that the "/bin" directory exists. Doesn't make any difference if I remove it, there's no existing installation of matplotlib. This gives me matplotlib version 3.0.3
Finally I start Python by /Blender/2.79/python/bin/python.exe, and do the above mentioned importmatplotlib.pyplot command which causes the crash.
I've tried this from a completely fresh installation of Blender, which comes with no existing external modules installed. Could there be some conflict with other Python modules elsewhere ? How would I go about diagnosing what's going on ?
Thanks for any ideas !
FIXED ! I remembered I found the solution months ago, but stupidly wrote down the answer in the wrong file...
The problem is that matplotlib is looking for a file that doesn't exist. In c:/users/me/.matplotlib (a hidden file), there's a file ""fontlist-vXXX.json", where XXX is the version number. This is set in line 951 of the file "font_manager.py", located in /python/lib/site-packages/matplotlib. In my case, the font_manager was looking for version 300 but the actual file was 310. Changing the version number in the font_manager.py made everything work correctly.

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

Which backend for matplotlib using MacOS?

The question of using matplotlib with MacOS is a tricky one which has already been thoroughly reviewed by a number of discussions (see below). The problem is the following:
using MacOS Mojave 10.14.3
using python 3.7.2 in a conda environment
using matplotlib 3.0.3
Here is the simplest code snippet I came up with which allows reproducing the issue:
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)
plt.show()
This throws the following error:
2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The issue is documented here. One solution is to install the PyQt5 package to your Python installation and to add the following lines at the beginning of your script:
import matplotlib
matplotlib.use("Qt5Agg")
While this works perfectly well, I am wondering why other backends fail to provide similar behavior.
Indeed I tried using MacOSX backend :
import matplotlib
matplotlib.use('MACOSX')
Which yields to the error:
from matplotlib.backends import _macosx
ImportError: 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.
The issue is documented here, there and in plenty of other threads.
Two solutions came out :
install python.app(conda install python.app) and launch your script with pythonw instead of python
use TKAggbackend
Using the first one works well but I wonder:
why do we need to call pythonw instead of python ?
what exactly is the python.app package ?
how can we make this solution work using an IDE (let say PyCharm for instance) ?
As for the second one, it does "work" up to a certain point: when running matplotlib using TkAgg, the plot window is really buggy. Indeed, it often needs several clicks on the "zoom", "pan" or "home" buttons to get them to actually work. It really is a great pain to use it. I asked several colleagues or friends using matplotlib with TkAgg and they all have the same issue.
Does anyone know the reason for this state of fact? Or if there is any workaround to avoid this issue (apart from installing pyqt5)?
Using the first option is your best bet since you are already working with a virtual environment.
According to matplotlib, there are two variants of python:
A Framework build - Quite important for GUI stuffs in MacOXs
A regular build.
Matplotlib in this case would want to interact natively with OSX and for this, it needs the Framework build this is the reason why installing the python.app type of python is important.
More information can be gotten from Matplotlib FAQ.
Check this link for more about the need for a framework build python.
I'm going to make some assumptions. If they're wrong I apologize.
You installed Python with Anaconda.
Personally, I've never had any problems on mac with matplotlib. My setup is: Mojave, Python3.7.3 in a venv using the python built in module (python3 -m venv), and matplotlib 3.0.3.
I can't answer your question on how to fix your problem, but I'm kind of trying to answer your "is there any workaround" question. Personally, I've always had issues with using Anaconda/Spyder/Conda for Python. I've always felt installing it as its own binary/app on the system leads to the fewest errors.
Now, I'm not saying you have to download and install by hand though. I use homebrew and it saves me headaches everyday I assume (such as upgrading applications and packages). That's the "work around" I'd suggest. Because isn't installing via Anaconda/Spyder already a workaround to installing Python properly? I've always felt performing one work around requires more workarounds for full functionality. Such as having to specify the matplotlib backend when it should be detected by default.
Obviously, I'm a little biased against that tool and that may be reflected in this answer, so take it with a grain of salt. Even though Conda is a legitimate tool that I think is useful, I find it annoying having to use both pip and conda when conda doesn't contain the packages I want.
There are two things you can try.
You can read Matplotlib info page on that, https://matplotlib.org/3.1.0/faq/osx_framework.html,
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.
And follow the instructions.
Or simply follow the error message you get when you try %matplotlib inline,
(...)
UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I did %matplotlib osx and have plt.imshow(myimg) working just fine afterwards.

Numpy cannot be accessed on Jupyter. Is it a problem with the path and how do I fix that?

This is probably a very basic question but I have not been able to solve it for some time.
My goal is to start using Python with Jupyter Notebook for data analytics.
I first downloaded Python 3.7 on OSx10.95. Then tried to download Anaconda, which failed a few times. Then I downloaded Miniconda and used Wing101. After that I could download Anaconda. However, I did not get Anaconda navigator to work.
Then I started using Jupyter Notebook from terminal. It works but there are a number of problems:
In Jupyter when I try to import pandas and numpy I get an error:
--------
<ipython-input-1-baf368f80de7> in <module>
----> 1 import pandas as pd
2 import numpy as np
~/anaconda3/lib/python3.7/site-packages/pandas/__init__.py in <module>
17 if missing_dependencies:
18 raise ImportError(
---> 19 "Missing required dependencies{0}".format(missing_dependencies))
20 del hard_dependencies, dependency, missing_dependencies
21
ImportError: Missing required dependencies ['numpy']
----------
Numpy is installed though, but it is probably in the wrong place.
Another problem is that the Anaconda and Python files are all over my computer:
The Anaconda navigator is at:
/anaconda3
Pip 3.7 is at:
/Library/Frameworks/Python.framework/Versions/3.7/bin/
Numpy is at:
/Users/lsluyser/Downloads/ENTER/lib/python3.7/site-packages/pandas/compat/
Jupyter files are at:
/Users/lsluyser/Downloads/ENTER/lib/python3.7/site-packages/
and also at:
/anaconda3/lib/python3.7/site-packages
My question is:
What is the desired organization of the program files and how do I achieve this?
Should I move all files from Downloads to another folder?
Should numpy be put under /anaconda3/lib/python3.7/site-packages?
Can the fact that Anaconda navigator does not work have to do with its location?
Thank you very much in advance!
I suggest using Miniconda, which is a smaller alternative to Anaconda. Even if you don't, you should download the packages you need, such as numpy, from
Anaconda Cloud, which should put the files in proper location.
Generally [on Windows] the packages should be in folder C:\Users\<>\Miniconda3\Lib\site-packages and verify the environment variable has necessary paths.
If you're going to work in Python, you will soon realize the need for creating multiple python virtual environments on your computer.
This is because, when working in Python:
You will constantly run into situations that require you to install, upgrade, or downgrade some new module.
Each such install, upgrade, or downgrade could have some unwanted side-effect (something that was working earlier, stops working after the change).
By creating multiple virtual environments, you will be able to perform such installs, upgrades or downgrades within a specific environment, with no risk of affecting your other environments.
Tools such as Anaconda and Miniconda make it easy for you to create and manage such virtual environments.
Under the hood, the creation and management of the virtual environments is probably not much more than setting some environment variables.
I found this to be a good intro to the concept.
For your problem, yes, most likely your problem with numpy can be solved by suitably setting environment variables, but I would suggest not to attempt that.
Instead, use Anaconda or Miniconda to create an environment, and within that environment, use Anaconda or Miniconda to install numpy. You will of course will be prompted about any pre-requisites that may be needed for numpy.

python newbie: importing numpy module in eclipse

I'm trying to run a simple python program in eclipse. This is the first time that I'm importting any module.
So I downloaded: numpy and pylab (I'm using a mac) restarted my eclipse and the red line below the
import numpy
import pylab
disappeared so I understood that the reference to that module is ok.
Problem is that I still see red line below the code and wonder why? I have to stress out that I believe numpy was already 'pre-installed' I just upgraded the version (using 1.5.1-py2.7).
Can anyone tell what should I do to run this code?
my interpreter setting on eclipse:
If you are using PyDev, you should first have to go to Preferences, then Pydev, then Interpreter Python and then Libraries to add NumPy.
Else, verify that you have NumPy installed, from the interpreter, just call from numpy import *
Edit:
Also check you already have Matplotlib installed, the error you are getting on the console points to that being the cause, you can download Matplotlib here.
I recently installed Anaconda3 and just started learning how to use Pandas and I wanted to be able to work with Pandas in Eclipse as well.
I first tried adding a reference to the site-libraries at:
C:\Anaconda3\Lib\site-packages
And it seemed to work by allowing me to import numpy and pandas. However, since I had just used conda to update everything, my Python34 interpreter seemed to fail when I tried running some code and numpy was looking for my Python35 installation. For some reason this was located at:
C:\Users\myname\AppData\Local\Programs\Python\Python35-32
However, Anacondas installed another version somewhere else. By going into:
Windows > Preferences > PyDev > Interpreters > Python Interpreter
and clicking on Quick Auto-Config it found my Anacondas version of Python35 and then I just had to figure out how to make my current project use the Python35 interpreter.
Hint: To do this, you need to go into the Project properties by opening the project and choosing File > Properties or right-click the project to choose Properties.
Simply removed the old numpy and installed version 6. located here
Another way to circumvent this problem is to use pip install numpy check how to install pip

Categories

Resources