Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert py file to exe
#11
(Jul-17-2020, 08:30 PM)SmukasPlays Wrote: When I execute any of the functions that are in the other files, they open but the CMD also open
It's not so common for .py files to execute outside the main Python file.
In a package or module usage it's most common that other files are imported an not run before called in main Python file.
I guess if want to execute those files on it's own,then need to make .exe of each them with --noconsole .
Then call the .exe and not .py file

To give a example what i talking about.
# life.py
def foo():
    return 42

if __name__ == '__main__':
    print(foo())
# meaning.py
import life

def bar():
    print(f'The meaning of life is {life.foo()}')

bar()
Run:
Output:
C:\code\a_folder λ python meaning.py The meaning of life is 42
life.py is not doing any executing on it's own but get get called in meaning.py.
So it increases functionality of the main file.
If change to this.
# life.py
def foo():
    return 42

print(foo())
Run:
Output:
C:\code\a_folderλ python meaning.py 42 the meaning of life is 42
No get life.py get executed on import,this rarely a wanted behavior.
Reply
#12
(Jul-18-2020, 12:22 AM)snippsat Wrote:
(Jul-17-2020, 08:30 PM)SmukasPlays Wrote: When I execute any of the functions that are in the other files, they open but the CMD also open
It's not so common for .py files to execute outside the main Python file.
In a package or module usage it's most common that other files are imported an not run before called in main Python file.
I guess if want to execute those files on it's own,then need to make .exe of each them with --noconsole .
Then call the .exe and not .py file

To give a example what i talking about.
# life.py
def foo():
    return 42

if __name__ == '__main__':
    print(foo())
# meaning.py
import life

def bar():
    print(f'The meaning of life is {life.foo()}')

bar()
Run:
Output:
C:\code\a_folder λ python meaning.py The meaning of life is 42
life.py is not doing any executing on it's own but get get called in meaning.py.
So it increases functionality of the main file.
If change to this.
# life.py
def foo():
    return 42

print(foo())
Run:
Output:
C:\code\a_folderλ python meaning.py 42 the meaning of life is 42
No get life.py get executed on import,this rarely a wanted behavior.

Oh ok, I tried it and worked. I had a little difficult but now everything is ok. Thanks!
Reply
#13
You can follow these steps to convert .Py to .Exe


pascals_triangle.py
---------------------------------
import time

n=int(input("Enter number of rows: "))
a=[]
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1,i):
a[i].append(a[i-1][j-1]+a[i-1][j])
if(n!=0):
a[i].append(1)
for i in range(n):
print(" "*(n-i),end=" ",sep=" ")
for j in range(0,i+1):
print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
print()

time.sleep(500)
--------------------------------------------------
1. Open up the command line
2. Navigate to the folder that contains the pascals_triangle.py program
3. Run pyinstaller pascals_triangle.pY



setup.py
----------------------------------------------------------
from cx_Freeze import setup, Executable

setup(name = "pascals_triangle" ,
version = "0.1" ,
description = "" ,
executables = [Executable("pascals_triangle.py")])
----------------------------------------------------------
To run cx_freeze, you can now:
1. Navigate to the folder containing pascals_triangle.py and setup.py
2. Run the command python setup.py build

When cx_freeze is done running the .exe file will be available in a directory within the folder that contains setup.py.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 870 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Convert File to Data URL michaelnicol 3 1,173 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,524 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,335 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,555 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  Convert legacy print file to XLSX file davidm 1 1,812 Oct-17-2021, 05:08 AM
Last Post: davidm
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,788 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,362 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,479 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Convert file of hex strings to binary file medatib531 4 13,756 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020