0

I have made a program in python that consists sys module. I want to convert that to an exe file using cx_Freeze. So I used the following code in the setup.py:

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

options = {"build_exe": {"includes": "atexit"}}

executables = [Executable("myfilename.py", base=base)]

setup(
    name="simple_PyQt5",
    version="0.1",
    description="Sample cx_Freeze PyQt5 script",
    options=options,
    executables=executables,
)

When I run the command: python3 setup.py build it builds the folder build inside that, it has a folder named "exe.win-amd64-3.9" inside that, it has a folder named lib, myfilename.exe, python3.dll and python39.dll in the exe.win-amd64... When I run the myfilename.exe it opens and closes whereas I have an input() it asks me for that. so can you please help me that how can I use the sys module in the file that I want to convert to exe using cx_Freeze

Athu R
  • 1
  • 1
    Does this answer your question? [cx\_Freeze doesn't print to console](https://stackoverflow.com/questions/38404800/cx-freeze-doesnt-print-to-console) – jpeg May 21 '21 at 07:52

1 Answers1

0

**Use cx_Freeze **

First check weather cx_Freeze library is installed or not, If not install by using pip install cx_Freeze else Upgrade the library by using

*pip install cx_Freeze --upgrade*

Import the code dependence into code

Create the code file labelled as Code.py

from datetime import datetime
import sys

print("Hello from cx_Freeze")
print(f"The current date is {datetime.today():%B %d, %Y %H:%M:%S}\n")

print(f"Executable: {sys.executable}")
print(f"Prefix: {sys.prefix}")
print(f"Default encoding: {sys.getdefaultencoding()}")
print(f"File system encoding: {sys.getfilesystemencoding()}\n")

print("ARGUMENTS:")
for a in sys.argv:
    print(f"{a}")
print()

print("PATH:")
for p in sys.path:
    print(f"{p}")
print()

**Create the run file labelled as run.py **

from cx_Freeze import setup, Executable

executables = [Executable("hello.py")]

setup(
    name="hello",
    version="0.1",
    description="Sample cx_Freeze script",
    executables=executables,
)

Update the code based on your convenience .

Athul A
  • 1
  • 1
  • Thanks, Athul A but it doesn't work. the window open's closes very fast – Athu R May 14 '21 at 15:26
  • Which os do you using. – Athul A May 15 '21 at 13:34
  • @AthuR Try to add `input("Press enter to exit")` at the end of the `Code.py` example of @AthulA to prevent the script from immediately closing, see [How to keep a Python script output window open?](https://stackoverflow.com/q/1000900/8516269) Or run your executable from within a `cmd` prompt. – jpeg May 21 '21 at 07:43
  • So as you said I added input("Press enter to exit") at the end of the script but when I create an exe file of it and run it, then It only asks me for the input("Press enter to exit")'s answer, it doesn't ask me for the sys input. – Athu R May 25 '21 at 10:54