python script that imports matplotlib succeeds butfrozen binary of script fails - python

My script needed to import numpy, sklearn, and matplotlib but I couldn't install sklearn. A very helpful response to my question http://https://stackoverflow.com/questions/38733220/difference-between-scikit-learn-and-sklearn explained that I needed to reinstall numpy. Using pip to update numpy failed because OS X 10.11 SIP prevented uninstalling the current numpy. The very helpful answer to a question about pip and SIP by mfripp http://https://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade provided a detailed solution to the problem. I followed those instruction exactly and used pip to reinstall numpy, matplotlib, scipy and sklearn for all users.
When I ran my completed script using the command
python DistMatPlot.py Random10A.matrix Random10A.pdf
the script ran perfectly, writing all expected output files.
However, I always saw:
"/Library/Python/2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.')"
which I had never seen with other matplotlib scripts before updating numpy, matplotlib, etc. The 2 second delay was only mildly annoying.
I compiled a frozen binary using pyinstaller and during the compiling I got several messages similar to that above.
The resulting frozen binary run failed with the command:
./DistMatPlot Random10A.matrix Random10A.pdf
produced the following:
/var/folders/8x/7_zp_33h8xj6td0059b72p9h0000gp/T/_MEIhIysTV/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. Traceback (most recent call last): File
"", line 13, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/pyplot.py", line 114, in File "matplotlib/backends/init.py", line
32, in pylab_setup File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/backends/backend_macosx.py", line 24, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 546, in load_module
module = imp.load_module(fullname, fp, filename, ext_tuple) 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 Working with Matplotlib in a virtual enviroment
see 'Working with Matplotlib in Virtual environments' in the
Matplotlib FAQ DistMatPlot returned -1
I have looked at similar questions and tried their suggested solutions to no avail.
(1) Why does matplotlib need to rebuild a font cache each time it is run?
(2) Why does the frozen binary fail when the script itself succeeds? Do I need some additional option other than -F when running pyinstaller?

The issue with the frozen binary turned out to be "RuntimeError: Python is not installed as a framework."
Several posts discussed that issue and suggested adding these two lines before "import matplotlib.pyplot as plt":
import matplotlib
matplotlib.use('TkAgg')
That did not work, but this slight modification did work:
import matplotlib
matplotlib.use('Agg')
I suspect that 'Agg" may be specific to either OS X or to the version of python that is included with OS X 10.11

Pyinstaller can have problems compiling some scripts, particularly if the use either the scipy or matplotlib modules. Compiling appears to succeed, but the compiled script may fail to execute. If the error message indicates that a module, e,g, scipy.stats, could not be found try compiling with the hiddenimport option:
pyinstaller -F --hiddenimport=scipy.stats --hiddenimport=_sysconfigdata GrowthRates.py
When compiling scripts that use scipy or matplotlib Anaconda python helps a lot:
OSX: python27Env
Windows: Use Anaconda python
Linux: Use Anaconda python

Related

Unable to import Pandas on Replit.com - Python

I'm unable to import pandas with import pandas as pd on replit.
I've already installed the package with pip install pandas and it can be seen in packages. I've successfully imported it to other projects on replit. Every time I try importing it into my code on this project, it gives me the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import pandas as pd
File "/home/runner/thing/venv/lib/python3.8/site-packages/pandas/__init__.py", line 16, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "/home/runner/thing/venv/bin/python"
* The NumPy version is: "1.22.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: libz.so.1: cannot open shared object file: No such file or directory
You don't need to use pip to install packages on repl.it -- and in fact, you shouldn't! Using Nix derivations not only works better (as you're using their OS distro the way it's designed), but also keeps their storage costs low, by allowing packages to be used from a read-only, hash-addressed, shared store.
Binaries built for other distributions might assume that there will be libraries in /lib, /usr/lib, or the like, but that's not how NixOS works: Libraries will be in a path like /nix/store/<hash>-<packagename>-<version>/lib, and those paths get embedded into the executables that use those libraries.
The easiest thing to do here is to create a new bash repl, but to add a Python interpreter to it. (I suggest this instead of using a Python repl because the way they have their Python REPLs set up adds a bunch of extra tools that need to be reconfigured; a bash repl keeps it simple).
Create a new bash repl.
Click on the three-dots menu.
Select "Show Hidden Files".
Open the file named replit.nix
Edit the file by adding a Python interpreter with pandas, as follows:
{ pkgs }: {
deps = [
pkgs.bashInteractive
(pkgs.python38.withPackages (p: [p.pandas]))
];
}
...changing that to taste (as long as they're getting software from a channel that has binaries for Python 3.9 or 3.10, for example, you can change python38 to python39 or python310).
Click the "run" button
In the new shell that opens, run python, and see that you can import pandas without trouble.
If, after you add a Python file to your repl, you can also change the .replit hidden file to make it run that file automatically on startup. Note that on NixOS, you should use #!/usr/bin/env python as your shebang, as PATH lookups are essential.

matplotlib RuntimeError: Python is not installed as a framework

This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot, I got:
Traceback (most recent call last): File "", line 1, in
File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 114, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",
line 24, in
from matplotlib.backends import _macosx 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 Working with Matplotlib
in a virtual enviroment see 'Working with Matplotlib in Virtual
environments' in the Matplotlib FAQ
I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.') objc[25120]: Class
TKApplication is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKMenu is
implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class
TKContentView is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKWindow
is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined.
I have no idea how to fix that. I'm not using a virtual machine.
PS: I found out that by adding:
import matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot, it seems to work. But adding those two lines of code every time is annoying...
What's going on, and how I can fix it?
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference:
https://github.com/tensorflow/tensorflow/issues/2375
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.

Attempting to install pydot for use with networkx in Python 3.5

I have only recently started using Python 3.5 on my mac, which is running Yosemite, and I am trying to use the module networkx to compile and generate some visuals for graphics/network models. I don't have all that much programming experience so I apologise if I leave out any details.
I have successfully installed networkx (am able to use commands therefrom) but am currently struggling to install one of the optional modules that facilitates the generations of plots: GraphViz. I tried to install GraphViz via running through the command prompt the setup.py install commands but GraphViz installation seemingly requires another module, PyDot (PyDot2 to be more specific: pydot2-1.0.33-py3.5.egg.info). Therein lies the problem because I cannot seem to get PyDot to work correctly - I keep getting the following error when running the networkx command that uses PyDot (test2.py is a test graphics model):
Traceback (most recent call last):
File "/Users/mainuser/Downloads/test2.py", line 6, in <module>
nx.draw_graphviz(G)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz
pos = nx.drawing.graphviz_layout(G, prog)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout
return pydot_layout(G=G,prog=prog,root=root,**kwds)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/networkx/drawing/nx_pydot.py", line 277, in pydot_layout
D=P.create_dot(prog=prog)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pydot.py", line 1802, in <lambda>
lambda f=frmt, prog=self.prog : self.create(format=f, prog=prog))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pydot.py", line 1966, in create
self.write(tmp_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pydot.py", line 1895, in write
dot_fd = file(path, "w+b")
NameError: name 'file' is not defined
After doing some research and playing around I feel like the issue is that the version of PyDot that I installed is not compatible with Python 3.5. However, I don't understand how this could be the case since I installed PyDot2 which says explicitly that it is compatible with Python 3+.
Any insight or directions would be much appreciated!
I noticed this issue and published a Python 3 compatible version on PyPi. The reason is that pydot/pydot2 uses functionalities that is removed in Python 3 already.
For Linux systems for Python 3.x, try:
pip3 install pydot3
Or in general for Python 2.x, try:
pip install pydot3

Need to use TkApp backend with matplotlib in Pyzo

I'm trying to put a live mass spectrum plot into a program that I am writing. So far I have a simple GUI that give the program its basic functionality. I also want it to have a live plot of a mass spectrum, a fairly simple 2D bar chart with about 10,000 peaks. I'd like the user to be able to zoom and pan around the plot but the default backend of matplotlib is really slow. I've been reading around and other backends are available and TkApp is recommended. I also want it to be compatible with tkinter Python 3.3.
When I try to change the backend:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy
print(mpl.pyplot.get_backend())
mpl.pyplot.switch_backend('TkAgg')
plot = plt.plot([1, 2, 3, 4, 5, 6])
plt.show(plot)
print(mpl.pyplot.get_backend())
Output:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
Qt4Agg
Traceback (most recent call last):
File "C:\MyPyProgs\Sequencer\spectrum_plot.py", line 7, in <module>
mpl.pyplot.switch_backend('TkAgg')
File "C:\pyzo2013c\lib\site-packages\matplotlib\pyplot.py", line 124, in switch_backend
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\__init__.py", line 28, in pylab_setup
globals(),locals(),[backend_name],0)
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\tkagg.py", line 2, in <module>
from matplotlib.backends import _tkagg
ImportError: cannot import name _tkagg
I'm using the Pyzo package. A couple of recommendations as fixes are to install python3-matplotlib-tk however it cannot find pyzo as it's a stand alone installation of python so is not in the registry. Any help would be much appreciated.
Thanks
First, it sounds like the Pyzo package is missing the pyparsing dependency, so that may be one cause of your speed issues.
Second, from the error message it looks like your matplotlib wasn't compiled with Tk support for some reason. You might want to contact Pyzo and see if they're aware of this.
As a workaround, I'd recommend first installing Python from python.org, then visiting Christoph Gohlke's Python Extension Packages for Windows repository. If you haven't already run across it, it's a very large and very up-to-date collection of scientific and other Python modules for Windows, packaged as self-installing .exe files. Many of the packages are linked to NumPy and SciPy compiled with Intel's Math Kernel Library (MKL), which can give significantly better performance in heavy-duty applications. Another nice thing about it is he gives dependency information where needed, so if you visit the matplotlib link it shows what other packages you'll need to download as well.
While you're there, I'd highly recommend checking out IPython, an interactive Python development environment that is just overflowing with features.
I have managed to fix this with the help of Almar from the Pyzo community group. I'm posting it here to help anyone else who might have the same issue.
So the first thing to do is to add pyzo to the registry, this is done by running pyzo_activate.exe. The cmd is then opened, I then answered y to all of the questions.
After this I downloaded the compatible version of matplotlib (and also PyQtGraph as it was recomended) and they could be installed as normal and recognise pyzo as the python version. now when the code from the question is run the following output is produced:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
TkAgg
TkAgg
This means that the default backend of matplotlib is now TkAgg. The pan/zoom controls for plot that is produced are smooth and responsive.

Beginner, Error in python regarding site-packages/celltool/numerics/fitpack/_fitpack.so

I'm doing some image analysis for my research, and it involves using CellTool (http://pantheon.yale.edu/~zp2/Celltool/), Python, and numpy. I'm on a OS X 10.8.3. On my old laptop that crashed, I was able to run my commands fine, but I got a new one and things are not going as smoothly!
I believe I have CellTool and numpy installed correctly, and I'm just using Python 2.7.2 that's standard on Mac. But when I try to run this python script "calculate_distances.py" command, I get this error:
Yuxins-MacBook-Pro:Modified_Contours yuxinsun$ python calculate_distances.py
Traceback (most recent call last):
File "calculate_distances.py", line 24, in <module>
normals = contours[n-1].inward_normals()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/contour/contour_class.py", line 384, in inward_normals
import celltool.numerics.fitpack as fitpack
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/__init__.py", line 1, in <module>
from fitpack import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/fitpack.py", line 34, in <module>
import _fitpack
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/_fitpack.so, 2): Library not loaded: /usr/local/lib/libgfortran.2.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/celltool/numerics/fitpack/_fitpack.so
Reason: image not found
And I have no idea what any of it means...I only need to run this python script and get the file that it spits out.
Am I just missing the "fitpack" whatever that is? If so, how do I install it? Or what can I do to fix this problem.
You need to make sure you have those installed per installation requirements:
If one of the above binary installers did not suit your needs,
please install Celltool from the source code.
This depends on only three things:
1.Python (version 2.3 or later).
2. NumPy 1.0.4 or later.
3. Working C/C++ and Fortran compilers.
and it looks like your Fortran compiler libs are not installed.
This might be helpful: https://scicomp.stackexchange.com/questions/2469/how-should-i-install-a-fortran-compiler-on-a-mac-os-x-10-x-x-4

Categories