0

I would like to include shapefiles in the .exe itself. When I run the following spec file, my .exe is created but I must include in the same folder my shapefiles in order to run my application. I was wondering if I can package my application with the list of shapefiles in order that my application is autonomous and does not require other files on the side.

The spec file:

# -*- mode: python -*-

block_cipher = None

a = Analysis(['map.py'],
             pathex=['C:\\Users\\...\\PyQt5\\Qt\\bin', 'C:\\Users\\...\\Applications'],
             binaries=[],
             datas=[('C:\\Users\\...\\Applications\\data', 'dir')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)


pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='XXX',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False )

FYI: I saved all my shapefiles at the following address: C:\Users\...\Applications\data

Thank you

H. Dave
  • 549
  • 3
  • 9
  • 26
  • The way I see it, by your spec file (this line: `datas=[('C:\\Users\\...\\Applications\\data', 'dir')]`), your shapefiles will be copied to a `dir` folder inside your installer. As in `your_project/dist/map/dir`. Is this what happens? Do you want those files to be in the `map` directory instead? – Daniel F. Feb 07 '18 at 03:36

1 Answers1

0

How are you referencing your external files in your python code? Once the application in bundled, the external files will be saved in a temporary directory, which you will need to reference. See this post for a discussion on referencing these external files. In short, you will need to update the path to your resource file before referencing it:

#resource_path is the relative path to the resource file, which changes when built for an executable
def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath('.')
return os.path.join(base_path, relative_path)

and in the body of your code:

file = resource_path('data.txt')
apogalacticon
  • 709
  • 1
  • 9
  • 19
  • I am referencing my external files in my python code with the following code: `map.readshapefile('shapefile, 'state')`. If I understand correctly, I add the first part of your code in my spec file and `file = resource_path('data.txt')` in my python file. Is that correct? Does `data.txt` represents the file that I want to add to my `exe` or is it a file where I list all external files such as shapefiles, icon,...? Thank you – H. Dave Feb 24 '18 at 16:17
  • You will need to add the `resource_path` function to your python file, and reference your files using the syntax described in your comment. `data.txt` represents the file that you want to open/use in your application. – apogalacticon Feb 25 '18 at 20:16