简体   繁体   中英

Loading datafiles with pyinstaller2.0

I have been working on an application which requires to load datafile (png files and pot files) using pyinstaller, and I've been tracking the files of the temp folder, the datafiles are not added to the temp folder for some reason. I made a small module to keep track of the directory which is as follow:

 import os, sys

 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)

I also modified the spec file:

a = Analysis(['mks_controller.py'],
             pathex=['C:\\pyinstaller-2.0\\pyinstaller-2.0'],
             hiddenimports=[],
             hookspath=None)
a.datas += [('presentation.potx','C:\\pyinstaller-2.0\\pyinstaller-2.0\\bbpresentation.potx','DATA'),('splat.png','C:\\pyinstaller-2.0\\pyinstaller-2.0\\splat.png', 'DATA'),('logo.png','C:\\pyinstaller-2.0\\pyinstaller-2.0\\logo.png','DATA')]
pyz = PYZ(a.pure)

I built the exe using the following two commands:

pyinstaller.py --onefile filename.py 
pyinstaller.py --onefile filename.spec

and the datafiles are not loading to the directory. I also tried to run a log which shows all files that are in this directory, the datafiles are not shown up. Here's a screenshot of it:

http://imgur.com/delete/qIff0zZT2Y4ZdKT

I'm using python 2.7 and pyinstaller 2.0. Anyone has any idea what the problem is? Thanks in advance!

Some suggestions from a noob who has recently gone through the learning curve. There are many things that can give errors. First get the exe working without including the data files...

1) Are you using windows? I made a batch file to run the command (seemingly hundreds of times untill I got it working) and 'pause' holds the window open to see the error messages.

pyinstaller.py --onedir filename.py
pause

2) I used --onedir. One file takes a while to unpack all the resources anyway, thus time wasted during testing. I put all my data files in a separate directory on the drive for testing and used the absolute path in my program. These can then be manually included in the dist directory and the relative path set accordingly in your program at a later date if you still have problems getting onefile to work. Address other errors before worring about editing the spec file and packing everything into onefile.

3) I didnt worry about editing the spec file because I didnt include data files!

4) I ran the executable via a batch file also (because im a noob and had to run it over and over again), again with the 'pause' command so the window stays open and you can read the errors.

5) Finally to fix the errors I had to switch to the latest pyinstaller development code (fixed the 'pythoncom' errors). The dev code seems better to help debug as it gives less 'warnings'. I also had to update a module (in my case pyproj) to the latest build. I would suggest the first thing you do to fix errors with your installed modules is update them. The various module maintainers are probably aware of any issues they are having with pyinstaller and may have addressed them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM