This question has been asked before and I can't seem to get my PyInstaller to work correctly. I have invoked the following code in my mainscript.py file:
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
When I run the py file (within IDLE), my app runs perfectly and loads all of the data files. However, when I bundle it with PyInstaller 2.1 (one file method) I get the following error after the exe builds:
Traceback (most recent call last):
File "<string>", line 37, in <module>
WindowsError: [Error 3] The system cannot find the path
specified: 'C:\\Users\\Me\\AppData\\Local\\Temp\\_MEI188722\\eggs/*.*'
Does anyone have any idea where I went wrong? Thanks!
** EDIT **
Here is exactly what I want to do.
My main script has a setup (imports) that look like below. Essentially I want to be able to have Matplotlib, Basemap, and resource path all in it:
import os,sys
import matplotlib
matplotlib.use('WX')
import wx
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import Calculate_Distance # A personal py file of mine
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
bmap=wx.Bitmap(resource_path('test_image.png'))
print 'hello'
I am using PyInstaller 2.1. I am also using Python 2.7.5 (32 bit). My OS is Windows 8 (64bit). My Matplotlib is 1.3.0 and Basemap is 1.0.6. Wxpython is 2.8.12.1 (Unicode).
I go to command and do: > pyinstaller myscript.py
. This generates my .spec file which I slightly edit. Below is my edited spec file:
data_files = [('Calculate_Distance.py', 'C:\\Users\\Me\\Documents\\MyFolder\\Calculate_Distance.py',
'DATA'), ('test_image.png', 'C:\\Users\\Me\\Documents\\MyFolder\\test_image.png',
'DATA')]
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
'Tkconstants', 'Tkinter']
packages = []
dll_excludes = []
dll_includes = []
a = Analysis(['myscript.py'],
pathex=['C:\\Users\\Me\\Documents\\MyFolder','C:\\Python27\\Lib\\site-packages\\mpl_toolkits\\basemap\\*'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries - dll_excludes + dll_includes + data_files,
name='MyApplication.exe',
debug=False,
strip=None,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=None,
upx=True,
name='MyApplication')
I want this to be a one-file executable so the data files should be packed within the executable. On other pyinstallers I usually haven't had issues with the MEIPASS. However, I need to use 2.1 because of Matplotlib and Basemap. If someone can build this exe perfectly -- can you please tell me what I need to adjust? Thanks!
****EDIT****
If anyone can figure out how to do this with py2exe -- that would be great too. Any way that I can get this into a single executable would be worth it!
Under macOS, PyInstaller always builds a UNIX executable in dist . If you specify --onedir , the output is a folder named myscript containing supporting files and an executable named myscript . If you specify --onefile , the output is a single UNIX executable named myscript .
The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.
PyInstaller looks for UPX on the execution path or the path specified with the --upx-dir option. If UPX exists, PyInstaller applies it to the final executable, unless the --noupx option was given. UPX has been used with PyInstaller output often, usually with no problems.
I think I see the problem. You're not feeding data_files into your Analysis object. Here's how I add my data files in my .spec file:
a = Analysis(....)
a.datas += [('7z.dll', '7z.dll', 'DATA')]
a.datas += [('7z.exe', '7z.exe', 'DATA')]
a.datas += [('collection.xml', 'collection.xml', 'DATA')]
a.datas += [('License.html', 'License.html', 'DATA')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts + [('O', '', 'OPTION')],
a.binaries,
a.zipfiles,
a.datas,
name=os.path.join('dist', 'blah.exe'),
debug=False,
strip=None,
upx=False,
console=True,
icon=r'..\NCM.ico')
Notice I'm not using COLLECT() at all.
If you checkout the 2.1 documentation at: PyInstaller Spec File Operation you'll notice that COLLECT() isn't used for --onefile mode.
If you look at the extracted directory pointed at by sys._MEIPASS you'll probably notice that with your spec file that the data file simply isn't there at all.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With