Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyinstaller : program that reads a csv

I'm building a program in python that reads a csv file given by the user.

I'm using Pyinstaller to build a MacOS Application.

In windows, I used CX_freeze and I just had to put the csv file in the same directory than the .exe for the program to work. But with PyInstaller, the file is not found Even if I put it in the same directory.

So where the user has to put the file in order for the program to find it ?

the program is a very basic one, something like this :

import pandas as pd

df = pd.read_csv('file.csv')

print(df)

Thanks.

like image 517
Ahmed Lahlou Mimi Avatar asked Apr 12 '18 08:04

Ahmed Lahlou Mimi


People also ask

Can Python parse CSV files?

Any language that supports text file input and string manipulation (like Python) can work with CSV files directly.

How do I convert a CSV file to Python?

DictWriter() class can be used to write to a CSV file from a Python dictionary. Here, file - CSV file where we want to write to. fieldnames - a list object which should contain the column headers specifying the order in which data should be written in the CSV file.

Is there an alternative to PyInstaller?

There are five alternatives to PyInstaller for Windows, Linux and Mac. The best alternative is nuitka, which is both free and Open Source. Other great apps like PyInstaller are py2exe, cx_Freeze, Shed Skin and bbfreeze.


1 Answers

When I create executables with Pyinstaller, I have to use the --add-data argument.

http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files

With this, you can specify the route for files like your .csv.

If you're creating a one-file bundle, Pyinstaller renames the paths internally [docs], so if you have your .csv inside some folder, you will need to do something like this each time you access to a file in your project:

def resource_path(relative):
    #print(os.environ)
    application_path = os.path.abspath(".")
    if getattr(sys, 'frozen', False):
        # If the application is run as a bundle, the pyInstaller bootloader
        # extends the sys module by a flag frozen=True and sets the app 
        # path into variable _MEIPASS'.
        application_path = sys._MEIPASS
    #print(application_path)
return os.path.join(application_path, relative)
like image 166
Daniel Rodríguez Avatar answered Oct 01 '22 15:10

Daniel Rodríguez