Using Mayavi on macOS with PyQT5 - python

I tried to setup Mayavi on macOS (currently running Sierra) using the following:
brew install --with-qt5 vtk
brew install mayavi
This should theoretically work but now when I try to use Mayavi in my code I get the following error:
Traceback (most recent call last):
File "test.py", line 32, in <module>
mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300))
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/figure.py", line 63, in figure
engine = get_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 101, in get_engine
return self.new_engine()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 146, in new_engine
check_backend()
File "/usr/local/lib/python2.7/site-packages/mayavi/tools/engine_manager.py", line 49, in check_backend
''')
ImportError: Could not import backend for traits
_______________________________________________________________________________
Make sure that you have either the TraitsBackendWx or the TraitsBackendQt
projects installed. If you installed Mayavi with easy_install, try
easy_install <pkg_name>. easy_install Mayavi[app] will also work.
If you performed a source checkout, be sure to run 'python setup.py install'
in Traits, TraitsGUI, and the Traits backend of your choice.
Also make sure that either wxPython or PyQT is installed. wxPython:
http://www.wxpython.org/ PyQT:
http://www.riverbankcomputing.co.uk/software/pyqt/intro
I belive the error lies in the fact that Homebrew only supports Qt5 and PyQT5 while Mayavi is looking for PyQT4 in the background. Is there a way to get Mayavi to work with PyQT5 or is there a way to install PyQT4 with brew?
And of course the problem could lie elsewhere....
Thanks!

An alternative is to use wx instead of qt, see the docs here: docs.enthought.com/mayavi/mayavi/installation.html
Then, set the environment variable export ETS_TOOLKIT=wx - it is worth a try.

I would recommend against using wx; I've run into a mess of issues getting it to run Mayavi examples (not to mention incompatibility with virtual environments). And from the mayavi repo:
Mayavi itself should work with the new wxPython 4.x. However, traitsui, pyface, and other ETS packages do not yet support it so the UI will not work correctly. Older versions should work. PyQt/PySide should work largely out of the box.
Here's how to install Mayavi w/ PyQt4 on macOS Sierra+:
brew install vtk
brew tap cartr/qt4
brew tap-pin cartr/qt4
brew install cartr/qt4/qt
brew install cartr/qt4/pyqt
brew install pyside
pip install mayavi
These steps make sure you get v4, not the default v5. And you may want to do unset ETS_TOOLKIT to make sure mayavi doesn't look for a different backend.
FWIW PyQt5 is not available (at least as a PyPI package) for Python 2x -- I see you're running Python 2.7. You can do pip3 PyQt5, but mayavi dependencies will need extra setup for Python 3 -- specifically pip3 install traitsui.

Related

Unable to import python modules (numpy, scipy, matplotlib)

I've used the following tutorial to install Python, numpy, scipy and matplotlib:
https://penandpants.com/2012/03/01/install-python-2/
I downloaded each and installed by double-clicking. I'm using a Mac with OS X Yosemite Version 10.10.5
When I try to import numpy, scipy and matplotlib in IDLE I get the follow error message:
`>>> import numpy
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
import add_newdocs
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/multiarray.so: no matching architecture in universal wrapper`
Can anyone please explain to me how to resolve this?
Thanks
use terminal and type
sudo pip install numpy
sudo pip install matplotlib
sudo pip install scipy
for python3+ use pip3
I can't say for sure what's wrong with your installation, but I suspect it's due to a some PATH-conusion... Python 2.7 and Numpy should be installed on your Mac by default.
If you run the command which python, you'll see which executable the python-commands invokes... is it the same as the one you installed?
If you write python in the terminal, and press tab twice, do you see multiple options of installed python versions?
Alternative method of installation
I prefer the Anaconda Python Distribution, which you can download like the full version or minimal version, which is the one I prefer.
If you install this, you should consider uninstalling the one you just installed.
Here's how to install the mini version:
Download the installer, and run it by opening a terminal in the folder with the installer, and running the command bash Miniconda2-latest-MacOSX-x86_64.sh. This will start the text-based installer, and if you read the instructions on screen, you'll be fine.
Notice that you have to scroll to the end of the licence agreement, before you can write yes when asked to accept the terms stated.
Say yes when it asked you if you'd like it to add Anaconda Python to your path.
Anaconda Python is different from other python distributions in the regard that it comes with it's own package and environment manager conda. With conda you can have multiple versions of Python installed at the same time (or several environments of the same Python version, but with different packages or versions of packages).
To create a new environment with the name py27, which is using Python 2.7, write conda create -n py27 python=2.7 --yes. Once created, activate the environment by source activate py27.
With the new environment now active, install the packages you need by using conda: conda install scipy matplotlib --yes.
I'd also recommend installing Ipython, which is an improved shell, and possibly the Jupyter notebook: conda install ipython --yes and conda install notebook --yes.
Hope it helps.

Pygame "No module called pygame.base"

When I download pygame-1.9.2b8-cp36-cp36m-win32.whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame I turn it into zip, take the headers in the pygame-1.9.2b8.data folder into pygame34/include/pygame and I put the other pygame file from the zip in the site-packages folder together with pygame-1.9.2b8.data as any tutorial shows, I always get this error:
Traceback (most recent call last):
File "C:/Users/andsa/PycharmProjects/THIS WORKS ANDREAS/Games.py", line 1, in <module>
import pygame
File "C:\Python34\lib\site-packages\pygame\__init__.py", line 141, in <module>
from pygame.base import *
ImportError: No module named 'pygame.base'
EDIT: I use python 3.4.5, I have tried both python and pygame 64 and 32 bit but i still get this error
This error bugged me for months, what i did was:
Uninstalled the package
pip3 uninstall pygame
Purged the pip cache
pip cache purge
Finally...Reinstalled pygame using pip3
pip3 install pygame
Worked like a charm :)
Use pip3 tool to reinstall the pygame python module:
C:\Python373\Scripts>pip3 uninstall pygame
C:\Python373\Scripts>pip3 install pygame
Then install it:
C:\Python373\Scripts>pip install pygame-menu==2.0.1
You probably have python installed for 32-bit. Uninstall and the install the 64-bit version. After that,
pip3 uninstall pygame
pip3 install pygame
try usin pip3 install pygames
to install pygames
pip3 install pygame
SUBLIME TEXT SOLUTION
OBS: THE REASON MOST GUI FRAMEWORKS DO NOT WORK ON SUBLIME IS BECAUSE OF THIS ONE LITTLE DETAIL HERE
Create a new build system:
In Sublime go to >> Tools
Tools >> Build System
Build System >> New Build System
DELETE EVERYTHING CONTAINED IN THIS NEW FILE AND ADD THIS PIECE OF CODE:
{
"cmd": ["/usr/local/bin/python3.8", "-u", "$file"],
"file_regex": "^[]*File \"(...*?)\", line ([0-9]*)"
}
ATTENTION: DO NOT CHANGE THE CURRENT DIRECTORY YOU ARE ON, WHICH IS THE
USER ONE.
SAVE IT AS Python3.8.sublime-build
In Sublime go to >> Tools
Tools >> Build System
Mark the Python3.8 you have just created
Voila!
P.S I Saved mine as 3.7 but it works the same, as long as the code inside the file is 3.8, or whatever version you have on your machine currently.

How to get matplotlib.pyplot working in virtual environment on OSX?

I first came across the answer to this question where I found out I needed to install my own backend framework. Since the answer mentioned PyQt4, I chose to go with that. Following links in the doc, I eventually ended up downloaded SIP (pre-req for PyQt4) and then PyQt4 from here. Finally, in my code, I have:
import matplotlib
matplotlib.use('Qt4agg') # need to call use() before importing plt
import matplotlib.pyplot as plt
However I'm still getting this error:
Traceback (most recent call last):
File ".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py", line 159, in
from PySide import QtCore, QtGui, version, version_info
ImportError: No module named 'PySide'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".../program.py", line 7, in
import matplotlib.pyplot as plt File ".../venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 114,
in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py",
line 18, in
from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py",
line 15, in
from .backend_qt5 import QtCore
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py",
line 31, in
from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, version
File
".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py",
line 162, in
"Matplotlib qt-based backends require an external PyQt4, PyQt5,\n" ImportError: Matplotlib qt-based backends require an external PyQt4,
PyQt5, or PySide package to be installed, but it was not found.
Has anyone experienced this before? Any debugging advice or help on where to go from here?
EDIT: I'll add that trying to import PyQt4 from my virtual environment works, so I'm not sure why matplotlib isn't finding it...
EDIT2: Not sure if it matters but I'm using PyCharm
I struggled with this for days and have finally arrived at a pretty simple solution after looking through loads of different stack overflow posts.
This is my solution for MacOSX and Python3.X, for someone who has already installed python3 via homebrew (i.e. brew install python3) and has virtualenv installed and a virtual environment already created with python3 (e.g. via virtualenvwrapper, mkvirtualenv myvenv -p python3):
(1) Install pyqt and sip using brew:
$ brew install sip --with-python3
$ brew install pyqt --with-python3
(2) Link the pyqt and sip files installed in /usr/local/Cellar/ (the default location for homebrew installations) to the site-packages directory in your virtualenv:
$ ln -s /usr/local/Cellar/sip/{SIPVERSION}/lib/python3.X/site-packages/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/
$ ln -s /usr/local/Cellar/pyqt/{PYQTVERSION}/lib/python3.X/site-packages/PyQt4/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/PyQt4
Make sure to modify the text in curly brackets {} as needed for your system, where VIRTUALENVHOME is the path to your virtual environment, VIRTUALENVNAME is the name of it, and SIPVERSION and PYQTVERSION are the versions of sip and pyqt that you downloaded. These can be found by simply looking in their corresponding directories /usr/local/Cellar/sip and /usr/local/Cellar/pyqt (the contents of these should be a single directory with the version number). Also don't forget to plug in the version of python3 you are using! (into all places where it says python3.X)
(3) pip install matplotlib in your virtualenv (if you haven't already) and modify the matplotlibrc so that matplotlib uses the PyQt4 backend instead of the default macosx backend:
# activate your virtual environment
$ workon VIRTUALENVNAME # if you are using virtualenvwrapper
$ #source ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/bin/activate # if you are not using virtualenvwrapper
# if you haven't already, install matplotlib
$ pip install matplotlib
# modify the matplotlibrc file to change the backend it is using
$ nano ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
This will open matplotlibrc in the default terminal text editor (alternatively you can use open -e instead of nano to open it in TextEdit). Go down to the first section after #### CONFIGURATION BEGINS HERE and change the line backend : macosx to backend : Qt4Agg. In the next section, uncomment the line backend.qt4 : PyQt4.
With this done, you should be up and running. You can test whether this worked with the following simple lines of code (assuming you have ipython installed in your virtual environment):
$ ipython -pylab
>>> import numpy as np
>>> plot(np.arange(10))
A plot should appear with a straight line. If this doesn't work, try reinstalling matplotlib in your virtual environment (i.e. pip uninstall matplotlib and pip install matplotlib).
Resolved this issue following this:
pip install matplotlib
There is a directory in you root called ~/.matplotlib.
Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg
Have you though about using conda instead of virtualenv?
conda create -n matplotenv matplotlib
source activate matplotenv
python -c "import matplotlib.pyplot as plt
p = plt.plot([1,2,3])
plt.show()"
source deactivate
I had the same problem, and what I did actually solved it (it might help if you didn't understand everything Jorge said!) :
1. go on anaconda navigator and launch qtconsole
2. type :$ brew install sip --with-python3
$ brew install pyqt --with-python3
3. copy the result of it in python, and execute.
Now try making a graph, it should work!

ImportError: matplotlib requires dateutil; import matplotlib.pyplot as plt

Am new to progamming and Python, i keep getting the error below when i run my program. Someone advised i should use pip to solve it. But cant pip get installed using the cmd. Though i suceeded using Powershell but still cant make it work. How do i solve this, any tips will go along way. Thanks
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from satmc import satmc
File "C:\Python27\starb_models_grid1\satmc.py", line 3, in <module>
import matplotlib.pyplot as plt
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 110, in <module>
raise ImportError("matplotlib requires dateutil")
ImportError: matplotlib requires dateutil
Am using version 2.7.3
You need to install various packages to get numpy working correctly.
libsvm-3.17.win32-py2.7
pyparsing-2.0.1.win32-py2.7
python-dateutil-2.2.win32-py2.7
pytz-2013.9.win32-py2.7
six-1.5.2.win32-py2.7
scipy-0.13.3.win32-py2.7
numpy-MKL-1.8.0.win32-py2.7
Matplotlib
Download all the binaries from this link and install and then you will have a working numpy installation.
You're probably looking for:
pip install python-dateutil
The error states that you are missing Dateutil.
This could mean that you did not install Dateutil with pip install python-dateutil or your way to install Python modules. But if you did your pip install and still get the same error it could also mean that DateUtil is installed in a folder where your Python distribution does not expect it.
In my case (Mac OSX) I installed pip with Brew http://brew.sh/. That pip will install the modules in: /usr/local/lib/python2.7/site-packages/
However the standard Mac OSX Python distribution will look for modules in /Library/Python/2.7/site-packages/
Your folders could be different of course, but you can check where Python looks for modules with:
import sys
print sys.path
Check this and see if one of the directories in the list contains the directory where dateutil is installed.
If not add a PYTHONPATH environment variable: Like: PYTHONPATH=/usr/local/lib/python2.7/site-packages/ or the folder where your dateutil is installed
I did pip install python-dateutil, but kept getting the same error. I got past that error by installing from http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-dateutil
Look for the Matplotlib section and install the dependencies you don't have.
Matplotlib is a 2D plotting library.
Requires numpy, dateutil, pytz, pyparsing, six, setuptools, and optionally pillow, pycairo, tornado, wxpython, pyside, pyqt4, ghostscript, miktex, ffmpeg, mencoder, avconv, or imagemagick.

Problems importing python-Xlib

I installed a new module and it appears as if one of its dependencies was not already installed. The module is called Xlib.display.
Here is the error message I received:
from Xlib.display import Display
ImportError: No module named Xlib.display
Where can I find this module that I am apparently lacking? Google yielded no leads.
"Edit: I already have that sourceforge module downloaded but I still get the same results.
Please try.
This shall install Xlib
sudo apt-get install python-xlib
Then you can check
>>from Xlib.display import Display
To install PyMouse if you want to control and capture mouse events please use:
sudo easy_install https://github.com/pepijndevos/PyMouse/zipball/master
Below worked for me!
pip install python3_xlib
I have also used pyuserinput for automation which requires this.
I was having the same problem, but the solutions above didn't work for me. Since I had installed python through the anaconda package, when I used:
sudo apt-get install python-xlib
Xlib was still undetectable by python2. The solution in my case was to use:
anaconda search -t conda python-xlib
Then find the package from the anaconda api, mine was erik/python-xlib. Install it using:
conda install --channel https://conda.anaconda.org/erik python-xlib
Then it worked.
On Debian systems install python-xlib.
On other systems there's a high probability that the package carries the same name.
I don't think the Xlib library works in Python 3.
Source:
Requirements
The Python X Library requires Python 1.5.2 or newer. It has been tested to various extents with Python 1.5.2 and 2.0 through 2.6.
I honestly cant explain why this works... but here is the command that got it working for me.
sudo apt-get install python3-xlib
Should not work because xlib apparently does not work with python 3.x, but everything installed alright, so I'm not complaining!
I was looking for the same answer, however after some more digging it seems that XCB (X protocol C-language Binding) will obsolete Xlib in general. From the XCB website:
The X protocol C-language Binding (XCB) is a replacement for Xlib featuring a small footprint, latency hiding, direct access to the protocol, improved threading support, and extensibility.
Fortunately there are python bindings available as python-xpyb in apt or xpyb on PyPi. I've not gotten that far in my project so I haven't tested if this works with Python3, but this is probably the way to go and the proper place to file any Python3 support bugs if necessary.
Scenario:
I was trying to use screenshot functionalities of pyautogui package. I was getting this error:
Traceback (most recent call last):
File "test_screenshot.py", line 1, in <module>
import pyautogui
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/__init__.py", line 152, in <module>
from . import _pyautogui_x11 as platformModule
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/_pyautogui_x11.py", line 7, in <module>
from Xlib.display import Display
ModuleNotFoundError: No module named 'Xlib'
Python code (test_screenshot.py):
import pyautogui
img = pyautogui.screenshot('test.png')
Environment:
Ubuntu 16.04 (LTS)
conda 4.5.11
Python 3.7 (Miniconda)
requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
PyTweening==1.0.3
Solution:
I installed python-xlib package in the conda environment using:
pip install python-xlib
Now test_screenshot.py is running without any error.
Updated requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
python-xlib==0.25
PyTweening==1.0.3
six==1.12.0

Categories

Resources