Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python program without 'python' in Windows [duplicate]

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.

like image 513
niladri chakrabarty Avatar asked Jun 15 '16 07:06

niladri chakrabarty


People also ask

Can I run a Python script in Windows without Python installed?

The only realistic way to run a script on Windows without installing Python, is to use py2exe to package it into an executable. Py2exe in turn examines your script, and embeds the proper modules and a python interpreter to run it.

Can Python programs be run without Python?

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.

Can you run the same Python file twice?

You can run multiple instances of a python script from a shell however from within a python program without the use of multithreading/multiprocessing the GIL limitation will impact what you are trying to do.


2 Answers

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

like image 135
Chandu codes Avatar answered Oct 16 '22 17:10

Chandu codes


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

like image 38
Melebius Avatar answered Oct 16 '22 19:10

Melebius