ImportError: No module named FileDialog - after PyInstaller - python

I have written a program that I have tried to turn into an executable using PyInstaller. Pyinstaller appears to have finished without any errors and I end up with an application in /dist/my_program. However, when I try to run that application a console window flashes up for a second with a traceback:
Edit: I have copied the traceback out. There may be a mistake as I had to type it up from a screenshot because it only flashes up.
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "C:\Users\user\desktop\PyInstaller-2.1\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\matplotlib.pyplot", line 108, in <module>
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\matplotlib.backends", line 32, in pylab_setup
File "C:\Users\user\desktop\PyInstaller-2.1\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\matplotlib.backends.backend_tkagg", line 7, in <module>
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\six", line 194, in load_module
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\six", line 108, in _resolve
File "C:\Users\user\desktop\PyInstaller-2.1\my_program\build\my_program\out00-PYZ.pyz\six", line 779, in _import_module
ImportError: No module named FileDialog
Below are the imports that I have in my code:
import Tkinter
from tkFileDialog import askopenfilename
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import subprocess
from PIL import Image, ImageTk
import os
Does anyone know whats causing this/what the fix is? I presume the error is the importation of tkFileDialog?
Edit2: the program runs fine when I run it in my interpreter (Spyder) but when I packaged it using PyInstaller the resulting application gives this error.

According to this question adding import FileDialog solves the problem. Matplotlib seems to need this.
However, I've used Pyinstaller on a script of mine also importing matplotlib and it gives no such error. So I don't know what exacly is the problem here.

Related

Failed to import any qt binding - Spyder on Windows 10

I'm trying to animate a trajectory of an object in 2D using python.
The problem is, when i try to do so I get this famous error
Traceback (most recent call last):
File "C:\Users\Pierre\OneDrive\Documents\Etudes\Master\M2\Stage\tests\sympy and gekko.py", line 13, in <module>
import matplotlib.pyplot as plt
File "C:\Python39\lib\site-packages\matplotlib\pyplot.py", line 2336, in <module>
switch_backend(rcParams["backend"])
File "C:\Python39\lib\site-packages\matplotlib\pyplot.py", line 276, in switch_backend
class backend_mod(matplotlib.backend_bases._Backend):
File "C:\Python39\lib\site-packages\matplotlib\pyplot.py", line 277, in backend_mod
locals().update(vars(importlib.import_module(backend_name)))
File "C:\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Python39\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 11, in <module>
from .backend_qt5 import (
File "C:\Python39\lib\site-packages\matplotlib\backends\backend_qt5.py", line 16, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File "C:\Python39\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 11, in <module>
from matplotlib.backends.qt_compat import QtGui
File "C:\Python39\lib\site-packages\matplotlib\backends\qt_compat.py", line 177, in <module>
raise ImportError("Failed to import any qt binding")
ImportError: Failed to import any qt binding
I tried every solution I found (installing PyQt5 and importing it, reinstalling matplotlib, updating pip) and nothing works. I still get the same error every time.
I'm on windows 10 and I installed python (3.9.2) using chocolatey. I'm coding with spyder 4.2.2
I've seen someone speaking about how updating his GPU driver helped but mine is up to date and still nothing (Radeon RX580).
Does anybody have a clue on what could be the issue ?
Installing Pyside2 did the trick. No need to for the import PyQt5 if you have the option picked in the preferences of Spyder

Failed to execute python script built using PyInstaller

Traceback (most recent call last):
File "site-packages\pandas\__init__.py", line 26, in <module>
File "C:\Users\adas\AppData\Local\Continuum\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pandas\_libs\__init__.py", line 4, in <module>
File "C:\Users\adas\AppData\Local\Continuum\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
File "pandas\_libs\tslibs\conversion.pxd", line 11, in init pandas._libs.tslib
File "C:\Users\adas\AppData\Local\Continuum\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
File "pandas\_libs\tslibs\conversion.pyx", line 1, in init pandas._libs.tslibs.conversion
ModuleNotFoundError: No module named 'pandas._libs.tslibs.np_datetime'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "fetchall2.py", line 5, in <module>
import pandas as pd
File "C:\Users\adas\AppData\Local\Continuum\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pandas\__init__.py", line 35, in <module>
ImportError: C extension: No module named 'pandas._libs.tslibs.np_datetime' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
[283516] Failed to execute script fetchall2
I am trying to execute a python script i wrote. The script is supposed to create a PNG file based on the plot created by matplotlib. It works fine when I run it on the spyder IDE, but fails when I build it with PyInstaller.
using these imports
import MySQLdb
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
import pandas as pd
from sklearn.preprocessing import Imputer, LabelEncoder, OneHotEncoder, StandardScaler
from sklearn import svm
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression
I tried to follow Petr Szturc's Method and created a hooks-pandas.py file and with hiddenimports = ['pandas._libs.tslibs.tslibs.np_datetime'] but continue to get the same traceback.
Anaconda 4.5.11
Python 3.6
I think I've had same error in the past.
I have a pyinstaller_hooks directory in my project, and file hook-pandas.py with the following code:
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('pandas._libs.tslibs')
Then when running PyInstaller I pass the directory in the --additional-hooks-dir:
pyinstaller --additional-hooks-dir pyinstaller_hooks your_python_file.py
This makes it easy to identify what changes I have made for hooks, instead of mixing them into the PyInstaller folder, and they also are checked into any git repo. I think this is much cleaner than the method proposed in your link.

build is successful but code doesn't work, throwing ImportError

My issue is little different from existing ones on the WEB. Let me try highlighting it in detail. I have this code to do some plotting by reading a .txt file specified. I was going to build it as usually as I did before. My setup.py is supposed to do the job. The content of setup.py is here
As you can observe I have no any package related to tkinter in both my above files. I did python setup.py build in terminal and it executed well. But can't use my actual code with windows command script
build\\exe.win-amd64-3.5\\draw_precision_recall_curve.exe D:\dataset\Fire_Smoke\Train\filelist.txt
pause
It throws this trace:
Traceback (most recent call last):
File "C:\python354\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\python354\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "draw_precision_recall_curve.py", line 3, in <module>
import matplotlib.pyplot as plt
File "C:\python354\lib\site-packages\matplotlib\pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\python354\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "C:\python354\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 4, in <module>
from . import tkagg # Paint image to Tk photo blitter extension.
File "C:\python354\lib\site-packages\matplotlib\backends\tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
File "C:\python354\lib\site-packages\six.py", line 92, in __get__
result = self._resolve()
File "C:\python354\lib\site-packages\six.py", line 115, in _resolve
return _import_module(self.mod)
File "C:\python354\lib\site-packages\six.py", line 82, in _import_module
__import__(name)
ImportError: No module named 'tkinter'
I was not able to solve this looking at the related posts here and there.
Any help is appreciated.
You may not be using tkinter, but you are using matplotlib, and it uses tkinter.
In fact, matplotlib gives you a choice of backends. But IIRC, the default is tkinter (or maybe TkAgg, which uses tkinter) on Windows if you haven't installed the optional Win32 native backend installed, and always on non-Mac Unix.
So:
If you're only using matplotlib non-graphically—e.g., to generate files to save—specify a non-interactive backend explicitly.
If you're using it to display graphs, you either need to bundle tkinter in your app, or pick a different interactive backend and bundle that in your app.
For example, let's say you just want to generate a bunch of graphs as PNG files. Instead of doing that through the default TkAgg backend, you can use the AGG non-interactive backend, like this:
import matplotlib
matplotlib.use('AGG')
import matplotlib.pyplot as plt

Python 2.7: Unable to import matplotlib.pyplot

When I tried to
import matplotlib.pyplot as plt
I was given the following error:
runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')
Traceback (most recent call last):
File "<ipython-input-2-9001323bba15>", line 1, in <module>
runfile('/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py', wdir='/Users/Zhengnan/Documents/Python/PS 2')
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/Zhengnan/Documents/Python/PS 2/230I_PS_2.py", line 4, in <module>
import matplotlib.pyplot as plt
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser
File "/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/mathtext.py", line 63, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so, 2): Library not loaded: #loader_path/../../../libpng15.15.dylib
Referenced from: /Users/Zhengnan/anaconda/lib/python2.7/site-packages/matplotlib/_png.so
Reason: image not found
Can anyone please help me? I'm a newbie and not really familiar with paths. A step-by-step instruction would be greatly appreciated. Thanks in advance.
BTW, I'm using spyder as an IDE.
as presented here:
https://askubuntu.com/questions/636937/python-2-7-matplotlib-provides-errorpyplot
this command will fix it:
conda install matplotlib
and indeed it helped me.
I use Kali Linux,I use:
pip install matplotlib
It's likely your library path is not set up correctly, and doesn't point to wherever the file libpng15.15.dylib is located.
Since this file should probably come with the anaconda installation (provided this is how you have installed Python and matplotlib), check if you can find this file in /Users/Zhangnan/anaconda/lib/.
If you find that file there, set your LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/Users/Zhangnan/anaconda/lib/
You may also want to add that line to your ${HOME}/.bashrc file.

Problem importing pylab in Ubuntu 8.1

I have installed numpy1.3,scipy 0.7.1,matplotlib 0.99.1.1 and python 2.5
when I import pylab I get the following error. Someone please help.
/var/lib/python-support/python2.5/gtk-2.0/gtk/__init__.py:72: GtkWarning: could not open display
warnings.warn(str(e), _gtk.Warning)
/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py:44: GtkWarning: gdk_cursor_new_for_display: assertion `GDK_IS_DISPLAY (display)' failed
cursors.MOVE : gdk.Cursor(gdk.FLEUR),
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/usr/lib/python2.5/site-packages/matplotlib/pylab.py", line 247, in <module>
from matplotlib.pyplot import *
File "/usr/lib/python2.5/site-packages/matplotlib/pyplot.py", line 78, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "/usr/lib/python2.5/site-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/usr/lib/python2.5/site-packages/matplotlib/backends/backend_gtk.py", line 44, in <module>
cursors.MOVE : gdk.Cursor(gdk.FLEUR),
RuntimeError: could not create GdkCursor object
try using a different backend for plotting than Gtk.
Open the python console and type:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
This will print a file name. Edit this file and modify the section 'Backend' and change Gtk or GtkAgg with any other (see the documentation in the same file), until you get it working.
You can find more information about backends here.
As for why you get an error when you try to use that backend, it depends on how you have installed the libraries and what you have installed on your computer. I would recommend to install matplotlib with synaptic/yum and selecting all optional dependencies.

Categories

Resources