0

I am trying to make a Python code executable so that the colleagues who do not have Python installed in their PCs can use it as well. Now, I have 2 questions: 1- How to make the Python code executable? 2- How to search for a specific format (lets say CSV) file in the directory of the exe file? I think this will require the directory to be detected first, then search within that directory for any file with the .csv format and read it.

Thank you,

MArablu
  • 31
  • 3
  • As for CSVs, you should ask in a seperate question but consider the CSV library and using built in classes like os and sys to browse files local to the install. – Theodore Howell Mar 14 '22 at 17:40

1 Answers1

0

You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.

PyInstaller Quickstart Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist.

pyinstaller -F yourprogram.py

Adding -F (or --onefile) parameter will pack everything into single "exe".

pyinstaller -F --paths=<your_path>\Lib\site-packages  yourprogram.py

running into "ImportError" you might consider side-packages.

pip install pynput==1.6.8

still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller

For a more detailed walkthrough, see the manual.

Theodore Howell
  • 419
  • 3
  • 12
  • I tried the "pyinstaller -F myprogram.py" but the generated file in the dist does not have an .exe format and it is not executing when I try it. – MArablu Mar 14 '22 at 18:32
  • @M.Arablu are you on windows or linux when running the pyinstaller? You will only get an .exe if you run it on windows. If you are on linux you also need to set permissions to execute. I would suggest reading the docs here, I used the program a few times to acheieve what you need: – Theodore Howell Mar 14 '22 at 19:04
  • https://pyinstaller.readthedocs.io/en/stable/operating-mode.html – Theodore Howell Mar 14 '22 at 19:04
  • generally I run the python in Linux through Visual Studio Code (IDE), but for the pyinstaller I ran it in Linux through ubuntu. – MArablu Mar 14 '22 at 19:53
  • @Theodore_Howell, Is there any way around the pyinstaller to generate an executable from the pyhton code on the Linux so that it can execute on Windows OS? – MArablu Mar 14 '22 at 20:44
  • @M.Arablu I currently do not have that answer, BUT you can try to rename the script to say .exe. That may be all you need to do. If you try to run the script as is on linux, you will have to make it executable so that it will actually run. In theory you should be able to do the same thing by renaming to script.exe and porting to windows. I am curious my self, let me know if it works? Best of luck! – Theodore Howell Mar 15 '22 at 00:43