Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Last active March 29, 2018 03:17
Show Gist options
  • Save peteristhegreat/f080f3dc8bb07c241e56a0091cd55412 to your computer and use it in GitHub Desktop.
Save peteristhegreat/f080f3dc8bb07c241e56a0091cd55412 to your computer and use it in GitHub Desktop.
NumPy, SciPy, MatPlotLib, PyQt, and Py2Exe all in one awesome example

Start with the directions here and get py2exe and pyqt4 installed with miniconda https://gist.github.com/peteristhegreat/0a05d7029befc5c2a302

conda install ...

Next install the rest of the good stuff

conda install matplotlib
conda install numpy
conda install scipy

Uber Hello World program

Make a simple hello world program that hits all the packages

See main.py below

Create the dist folder

See setup.py below. It is loosely based on:

python setup.py py2exe

See the sample output below

Resolve dependency errors

How to fix mkl_intel_thread.dll error!

https://stackoverflow.com/questions/34985134/py2exe-mkl-fatal-error-cannot-load-mkl-intel-thread-dll

Go to C:\path\to\miniconda\pkgs\mkl-2018.0.2-1\Library\bin and copy 4 dll's into your dist folder that py2exe created.

  • mkl_core.dll
  • mkl_intel_thread.dll
  • mkl_mc3.dll
  • mkl_rd.dll

Note, that if other dll errors crop up, use depends if you can, or you can also use a dirty trick that involves deleting folders in windows and let windows tell you where the dlls are.

I've used it many times for Qt deployments in the past, and I wrote it up here: https://stackoverflow.com/questions/17736229/do-i-have-to-include-all-these-qt-dlls-with-my-application

Here is my summary on the process:

  1. Make a backup of the folder with a bunch of dll's you think python might be using
  2. Run you python program
  3. While it is up and running delete the dll folder
  4. Windows will prevent you from deleting the folder
  5. Step into the folder and try deleting all the individual dlls
  6. Windows will prevent you from deleting the dlls in use!
  7. Copy all the dlls that are left to live right next to your main.exe
  8. Close your app, delete the partial dll folder, and restore your backup you made
  9. Profit!

SciPy ImportError ...

So run main.exe in the command prompt over and over after each py2exe dist build. Add in lines to setup.py under includes to catch all the sub features of scipy you are using.

For example, I was getting the runtime error: Missing messagestream.

Googling "scipy messagestream" found this:

https://stackoverflow.com/questions/47055712/error-when-executing-compiled-file-no-module-named-scipy-lib-messagestream

Then I added scipy._lib.messagestream to the includes list and it didn't have that error any more!

Hope that helps.

import sys
from PyQt4 import QtGui
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
print matplotlib.get_py2exe_datafiles()
def window():
print 'in window'
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
b = QtGui.QLabel(w)
b.setText("Hello World!")
w.setGeometry(100, 100, 200, 50)
b.move(50, 20)
w.setWindowTitle("PyQt")
w.show()
#sys.exit(app.exec_())
print 'in window'
app.exec_()
print 'in window'
def mpl_plot():
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.xlabel('other numbers')
plt.title("matplotlib")
plt.show()
def numpy_test():
x, y = np.arange(5), np.arange(5)[:, np.newaxis]
distance = np.sqrt(x ** 2 + y ** 2)
plt.pcolor(distance)
plt.colorbar()
plt.show()
def scipy_test():
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x ** 2 + 10 * np.sin(x)
x = np.arange(-10, 10, 0.1)
plt.plot(x, f(x))
############################################################
# Now find the minimum with a few methods
from scipy import optimize
# The default (Nelder Mead)
print(optimize.minimize(f, x0=0))
############################################################
print(optimize.minimize(f, x0=0, method="L-BFGS-B"))
############################################################
plt.show()
if __name__ == '__main__':
print 'top of main'
window()
print 'after window'
numpy_test()
scipy_test()
mpl_plot()
print 'bottom of main'
from distutils.core import setup
import matplotlib
import py2exe
setup(
# windows=[{ # Run in a window without any print statements
# "script": "main.py"
# }],
console=[{ # Run in a console and show print statements when running
"script": "main.py"
}],
options={
"py2exe": {
"includes": ["sip", 'scipy', 'scipy.optimize.*', 'scipy.integrate', 'scipy.special.*', 'scipy.linalg.*', 'scipy._lib.messagestream'],
'excludes': ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter', 'pydoc', 'doctest', 'test', 'sqlite3', 'zmq'],
'packages': ['matplotlib', 'pytz'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll',
'libgtk-win32-2.0-0.dll',
'libglib-2.0-0.dll',
'libcairo-2.dll',
'libpango-1.0-0.dll',
'libpangowin32-1.0-0.dll',
'libpangocairo-1.0-0.dll',
'libglade-2.0-0.dll',
'libgmodule-2.0-0.dll',
'libgthread-2.0-0.dll',
# 'QtGui4.dll', 'QtCore.dll',
# 'QtCore4.dll'
],
}
},
data_files=matplotlib.get_py2exe_datafiles(), #.extend([('.', [u'C:\\miniconda2\\pkgs\\mkl-2018.0.2-1\\Library\\bin\\mkl_intel_thread.dll'])]),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment