1

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?

Evgeny Mamaev
  • 1,237
  • 1
  • 14
  • 31
  • 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? – NotAName Jan 08 '20 at 05:19
  • @pavel this is what I wanted to ask. How can I specify the place where the file would be saved? – Evgeny Mamaev Jan 08 '20 at 05:28
  • It's hard to say without seeing the code. – NotAName Jan 08 '20 at 05:30
  • 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. – Evgeny Mamaev Jan 08 '20 at 05:34
  • 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? – NotAName Jan 08 '20 at 05:39
  • It sounds like you need to separate your application data from the application binaries/source. On Windows, I'd recommend LocalAppData, in which case, [this question](https://stackoverflow.com/a/52534405/7675174) will help you. – import random Jan 08 '20 at 05:48
  • @importrandom I considered it but I don't have access to the environment variables on the machines of my users. The intention is to create an executable file which can be run anywhere without additional requirements. – Evgeny Mamaev Jan 08 '20 at 05:53
  • @pavel this is what I'm going to do next, try other solutions. – Evgeny Mamaev Jan 08 '20 at 05:54
  • See [this answer](https://stackoverflow.com/a/1088459/7675174) about storing files in a cross-platform compatible way. – import random Jan 08 '20 at 06:00
  • Why is the directory different from where the script tries to save it? The directory should be the same – Mars Jan 08 '20 at 06:09
  • @Mars I assume due to PyInstaller creates a single .exe file Windows overrides the path from where the file was executed to some temporary place. – Evgeny Mamaev Jan 08 '20 at 06:17
  • The pwd should be exactly where you executed it. How are you creating the file? – Mars Jan 08 '20 at 06:18
  • I just created a short example that generates a file in the same directory that the exe is run in. You need to show us how you're creating the file and how you're calling the exe – Mars Jan 08 '20 at 06:24
  • @Mars the command I create the .exe with is already shown `python -m PyInstaller --hidden-import=timeit --hidden-import=bisect -F test.py`. I run the .exe from the command line `test.exe`. Can you share your short example? – Evgeny Mamaev Jan 08 '20 at 06:46
  • What is the code you are using to create a file? – Mars Jan 08 '20 at 06:48
  • I answered with the code needed to generate an example – Mars Jan 08 '20 at 06:56
  • Also, what is the original error? You only wrote the error you have when trying to use the add-files flag – Mars Jan 08 '20 at 07:03
  • And what was the call you used when using the add-files flag? – Mars Jan 08 '20 at 07:03
  • @Mars I don't use the add-files flag when I get the specified error. I simply don't know how to use it. The error arises with the use without the add-files flag. – Evgeny Mamaev Jan 08 '20 at 07:51
  • What error? You need to give us much more info – Mars Jan 08 '20 at 07:57
  • The error is `Error: [Errno 2] No such file or directory: "C:\Users\Admin\AppData\Local\Temp\..."` – Evgeny Mamaev Jan 08 '20 at 08:01
  • That's the error when you call the exe? Not when calling the add-files exe? – Mars Jan 08 '20 at 08:06
  • What is the value of `path`? I mean what are you passing into it? – Mars Jan 08 '20 at 08:06

1 Answers1

2

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!

Mars
  • 2,505
  • 17
  • 26