1
votes

I created a .exe file with Pyinstaller. The program should create a data file with the results at runtime. It works in the Python interpreter mode when I execute python test.py but it doesn't work in the .exe mode. It simply doesn't create a file and responds with an error.

Which flags should I add to the command python -m PyInstaller --hidden-import=timeit --hidden-import=bisect -F test.py to make it work?

The exception with this setup is:

Error: [Errno 2] No such file or directory: "C:\Users\Admin\AppData\Local\Temp\..."

Where the aforementioned directory is temporary and I don't have access to it.

The piece of code which is supposed to write a file is:

def write_file(data, path, encoding='utf-8'):
'''Creates report files.'''
try:
    if config.python_version == 2 and type(data) in (list, str):
        f = io.open(path, 'wb') 
    else: 
        f = io.open(path, 'w', encoding=encoding, newline='')
    if type(data) is list:
        writer = csv.writer(f)
        writer.writerows(data)
    else:
        f.write(data)
    f.close()
    console(u'Report file: ' + path)
except IOError as e:
    console(e, level=Level.error)

I assume there should be a setting to point to the place where the file should be saved.

I've checked here https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle but without success. I couldn't use the listed flags properly, nothing seemed to work.

How can I specify the place where the file would be saved?

1
I assume this folder in the error message is where the script attempts to save the file? Can you manually specify where the file is to be saved? - pavel
@pavel this is what I wanted to ask. How can I specify the place where the file would be saved? - Eugene Mamaev
It's hard to say without seeing the code. - pavel
I mean it doesn't depend on the code in this case. The PyInstaller decides where to put an output file. The directory where it tries to put it is different from the one where the Python script itself saves it. - Eugene Mamaev
In this case I have no idea. It seems like a very questionable functionality of pyinstaller, I can't think of a situation where I'd prefer for compiler to decide where the script output will be placed. Have you tried creating .exe with any other tool like py2exe, cx_Freeze or some others? - pavel

1 Answers

2
votes

The issue isn't with pyinstaller, it's with however you're creating your file.
You may be using some environment variable when running your python script from the command line that isn't set when you run your Exe


I have created a simple example of a program that creates a data file in the directory from which it is called:

#myscript.py
f = open("Test.txt", "w")
print("Hello World!", file=f)

Then I generate the Exe with Pyinstaller:

pyinstaller -F myscript.py

Copy the exe anywhere and you can create Test.txt, if you have permissions in that folder.

Double click myscript.exe
Test.txt appears in the same folder as myscript.exe

Hello World!