8

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.

Community
  • 1
  • 1
niladri chakrabarty
  • 503
  • 1
  • 7
  • 15

3 Answers3

8

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

Chandu
  • 2,053
  • 3
  • 25
  • 39
  • you should elaborate on your link (summarize) otherwise this answer is not worth much – DomTomCat Jun 15 '16 at 07:34
  • well it used to be just a link - probably it happened just then ... now it looks better. Btw it doesn't mean links aren't allowed - you can still provide the link for an additional read or as a reference – DomTomCat Jun 15 '16 at 07:44
  • Checking the edit history this was indeed mostly likely down voted because it was just a link, and the people who down vote usually don't get back to these to up vote again. So I threw in a up vote because others who search on the Internet will probably end up here and this will help a few of them and is not worth the negative votes that might scare people off from this type of solution. – Torxed Jun 15 '16 at 07:51
  • I don't need to convert the python script to exe. I want it to run without the 'python' keyword, as explained in the question body. Updated the misleading title. – niladri chakrabarty Jun 15 '16 at 08:28
2

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

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • I don't need to convert the python script to exe. I want it to run without the 'python' keyword, as explained in the question body. Updated the misleading title. – niladri chakrabarty Jun 15 '16 at 08:28
0

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.

Tino A.
  • 232
  • 3
  • 12
  • 3
    Don't think that's what the OP wants. It looks more like they just want to type `myscript.py` instead of `python myscript.py` a very common request. – SiHa Jun 15 '16 at 07:36
  • I don't need to convert the python script to exe. I want it to run without the 'python' keyword, as explained in the question body. Updated the misleading title. – niladri chakrabarty Jun 15 '16 at 08:27