1

Hi I noticed that whenever from the command line (using windows 8.1) I type

python file.py

It automatically knows that I meant to write python.exe file.py

How does it do this?

I installed Anaconda, and I understood I have an environment variable pointing to python.exe. But that doesn't explain why I need not type python.exe everytime.

1
  • When you type command, Windows automatically searches the PATH for command, command.exe, and command.bat. Commented Aug 24, 2015 at 20:08

2 Answers 2

1


This is not a python feature. The behavior to call executables without file extension is defined by the operation system and the PATH variable. Wikipedia has a good answer to your question PATH (variable)

...
When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right, looking for an executable filename that matches the command name given.
...

The file name after the executable could be everything. So if you want you can call python demo.txt. If the file content is readable for python it would also be executed.

Sign up to request clarification or add additional context in comments.

1 Comment

That is right but not the whole truth. Windows has made at least 3 ways of finding the executable. Two ways using the registry using the extension and the old way of using the current durectory and PATH. The last has precedence of the two former ways
1

Python is searched for using the path. Find the PATH in the list of environment variables.

If you do not write python and simply double-clicks the file, the registry is searched.

You can see what and associate files with program using ftype and assoc from the command line. See e.g. http://www.fileformat.info/tip/microsoft/assocftype.htm

If you omit the extension the registry is also searched. This cmd shell searches the environment variable PATHEXT and the registry for finding python.exe. After this, it is using the registry to find the location.

When you register your python distribution from within Spyder, these changes are made.

You can reveal this information by using ftype and assoc like I wrote in the beginning.

1 Comment

The registry association for the .py extension allows running just file.py. The OP wants to know why the ".exe" isn't necessary in "python.exe". That's because the cmd shell uses the extensions listed in the PATHEXT environment variable to find python.exe. You can add .PY to this environment variable, and combined with registry association, then you can simply run file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.