Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a python file (.py) from the windows command-line without having to type python first?

Suppose I have a python file named file.py. Normally to run this file from the command-line I would do:

python path\to\file\file.py

My question is, is it possible to do this without having the python before the file path like so:

path\to\file\file.py

Or, if I have the path to file.py in my Environment Variables, simply just:

file.py

I suppose it's worth noting I want to do this with a python file that is going to accept command-line arguments. Thanks :)

like image 379
RobertR Avatar asked Oct 19 '22 02:10

RobertR


1 Answers

The problem that you are facing is the fact that your python application is not actually an application. It is an interpreted script. This is because Python is an Interpreted Language.

This would be similar to you have a Word or Excel document. These are interpreted by their applications: Word and Excel, respectively. The operating system knows what application to use to interpret them using the registered associated programs.

The official Python FAQs explains this here: https://docs.python.org/2/faq/windows.html#how-do-i-make-python-scripts-executable

like image 71
caylorme Avatar answered Nov 01 '22 14:11

caylorme