Search code examples
pythonpandaspyinstallernsisgeopandas

PyInstaller & NSIS Zip2Exe - Referencing files outside of your PC


I created an application that references multiple shapefiles. Example below:

df = gpd.GeoDataFrame.from_file(r'C:\PATH_ON_OWN_PC\FILE_NAME.shp')

As you can see from the above, the program references files from my own PC.

I then used NSIS to create a setup.exe file so the program can be downloaded and used with other PCs. I included all the datasets in the zip file.

The issue is when the application runs the script it's still referencing the path on my own PC, which obviously won't work.

Is there a way to alter the code so it reads the files from the downloadable .exe file, so it reads the files no matter the PC it's downloaded from.

Thanks!


Solution

  • You should be able to use something like this so that your .exe will work on other computers. Here is a generic example that points to a file inside of my Scripts dir, where my python.exe is located, for one of my Conda envs. You can replace the second and third params in os.path.join() to match up with the path of your shapefiles.

    import os
    import sys
    
    python_exe_dir = os.path.dirname(sys.executable)
    sample_file_path = os.path.join(python_exe_dir, 'Scripts', 'gdal2tiles.py')
    print(sample_file_path)
    
    'C:\\Users\\matth\\anaconda3\\envs\\gpd_0\\Scripts\\gdal2tiles.py'
    

    So in your case, you'd end up with something like this.

    df = gpd.GeoDataFrame.from_file(sample_file_path)