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?

4
  • 1
    have you tried using the full path instead of the relative path? full path being c:\username\folder\file.csv? Commented Mar 9, 2020 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 Commented Mar 9, 2020 at 18:00
  • Could you share the exact error message? Commented Mar 9, 2020 at 18:26
  • Go ahead and play around with this, maybe print it even: basedir = os.path.dirname(sys.argv[0]) Commented Mar 9, 2020 at 18:27

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
Are you executing the exe from that folder? Or are you running it from shell? Did you try it on windows?
Is your exe composed by multiple files/folder? Or just one?
just one but more .py files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.