2

i'm writing a software that reads a csv file at after some steps creates another csv file as output, the software is working fine but when i try to create an executable with pyinstaller i have an error saying that my software can't find the input csv file. Here is how i am reading the csv file as input, i've also tryed to change the pathname with no luck:

import pandas as pd
def lettore(): 
  RawData = pd.read_csv('rawdata.csv', sep=';')
return RawData

how can i solve the problem?

lorenzo
  • 21
  • 1
  • 1
    have you tried using the full path instead of the relative path? full path being `c:\username\folder\file.csv`? – MattR Mar 09 '20 at 17:55
  • i'd like not to use the full path because i have to use this software in different computers and the path of my csv file could change it is better that it could read it from the same folder where the executable is – lorenzo Mar 09 '20 at 18:00
  • Could you share the exact error message? – Serge Ballesta Mar 09 '20 at 18:26
  • Go ahead and play around with this, maybe print it even: basedir = os.path.dirname(sys.argv[0]) – Aiyion.Prime Mar 09 '20 at 18:27

1 Answers1

1

Your code searches for the file it the same folder where the exe is launched.

It is equivalent to

import os
import pandas 

filepath = os.path.join(os.getcwd(), 'filename.csv')
df = pd.read_csv(filepath)

Do not use relative paths when you create an exe.

I can give you two other options:

  1. Use an input to get the right file path when running the exe (or eventually use argparse).
filepath = input("insert your csv: ")
df = pd.read_csv(filepath)
  1. Define an absolute path and build it in your code (you cannot change it after building and the program will read the file only from that path).

Edit: after reading your comment, see also

How to reliably open a file in the same directory as a Python script

ionpoint
  • 861
  • 6
  • 10
  • I tried to print the os.getcwd() output and it seems that my executable is running outside the folder where it is (i'm on mac) it is running in /Users/user insetad of in /Users/user/desktop/correctfolder – lorenzo Mar 09 '20 at 18:46
  • Are you executing the exe from that folder? Or are you running it from shell? Did you try it on windows? – ionpoint Mar 09 '20 at 19:19
  • Is your exe composed by multiple files/folder? Or just one? – ionpoint Mar 09 '20 at 19:26
  • just one but more .py files – lorenzo Mar 09 '20 at 19:32