8
votes

I have written a simple python script, and I need to run it as an executable, i.e., without the 'python' word at the beginning of the program. The script (simple_prog.py) is :

#!C:\Python27\python.exe

print "Hello World"

Whenever I am running the script as 'python simple_prog.py', it is printing the output alright, but without that, it is printing nothing (image below).

image

I have also referred to the "How do I make Python scripts executable?" section in the site https://docs.python.org/3/faq/windows.html#id3, from the stackoverflow question How to make python scripts executable on Windows? , but could not understand the solution.

Thanks.

Update :

Solved the problem from the stackoverflow link : Set up Python on Windows to not type python in cmd

This is what I followed (image below)

enter image description here

Please note the position of double-quotes in the ftype command.

The second command (simple_prog) ran successfully because I updated the PATHEXT variable by adding ".PY" in it.

Thanks for the responses as well as the downvotes.

3
what exactly are you looking for convert .py to .exe or you want to run the program without usingpython keyword - shivsn
Convert .py to .exe and make a extension name association are two different things. From your question I guess you ask the latter, but from the title you are asking the former. - Shuangistan
Updated the title. I am asking for the latter - niladri chakrabarty

3 Answers

7
votes

Prerequisites: Install py2exe v0.6.9 which is compatible with python 2.7

Follow below Steps:

1.Save your code in to test.py (or any name with .py extension) file. Make sure that the code works fine by running it using python.

2.Then create a new file with name setup.py and paste the following code in to it.

from distutils.core import setup
import py2exe
setup(console=['test.py'])

3.Now it is time to run the script and create the executable. To build the executable, run "python setup.py py2exe" on the command prompt.

4.After Building the executable is finished. Now you can find test.exe in the \dist sub folder. Move to dist sub folder and run test.exe, you can see output in console.

Hope it helps you..!!

Thanks

2
votes

You should associate .py files to be run in Python. Of course, this approach requires Python to be installed, unlike converting to .exe.

0
votes

I believe this is the solution you are looking for. http://www.py2exe.org/

It basically does what it says on the lid. You can create a windows executable from your python script following the tutorial on their page.