0

I have created a simple program for Windows in Python (converted to .exe with Pyinstaller) that copy itself to a created folder in Program Files. The program works perfect except one unforeseen detail, when the file copy itself to the directory it replaces the .exe extension with .py which makes the file unfunctional. Why is this happening?

Here is a very simple example that illustrate the problem:

import os
import shutil

filePath = os.path.abspath(__file__)
folder = 'some folder in C'

shutil.copy(filePath, folder)

Convert it to .exe in Pyinstaller with the following:

Pyinstaller --onefile name-of-file.py

When you run the program it will create a .py copy...

Lavonen
  • 606
  • 1
  • 10
  • 21
  • This might help? https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python – Ctznkane525 Dec 27 '17 at 23:18
  • `__file__` is the path of the module being executed, which is a `.py` file. If you name your `.exe` file the same as your module, you could swap out the filename extensions. – kindall Dec 27 '17 at 23:26
  • How do I do that? Is it the 'procedure' described in the link frm John Kane above? – Lavonen Dec 27 '17 at 23:29
  • How do I do that? Is it the 'procedure' described in the link frm John Kane above? – Lavonen Dec 27 '17 at 23:30

1 Answers1

2

Python is an interpreted language. PyInstaller/Py2exe/... are zipping the all python modules and add an executable header to make the exe-file executable.

When you are running the exe, all python modules are extracted to a temp directory and executed from there.

You can use sys.executable, to get the path of the exe. More details docs here.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47