lmiguelvargasf
lmiguelvargasf

Reputation: 69755

When running a python script from the console what is the difference between py vs python

I have been reading about python, and I have certain doubts when using the shebang line. When I run:

py file_name.py

It executes the script using the python version that I have indicated in the shebang line, but when I execute,

python file_name.py

The last version of Python is used instead of the one that I have specified. I would like to know the difference between using python and py when running a script from the command line. My shebang line was #! python3.4

Does using either python or py have different implications or eventually both are the same?

Upvotes: 2

Views: 850

Answers (2)

dotancohen
dotancohen

Reputation: 31481

To see what version of Python are in use for each command, run these two commands:

python -c "import sys;print(sys.version)"
py -c "import sys;print(sys.version)"

If both show the same version, then on your system they are both currently the same. However, one or the other might be updated. I personally recommend to rely on neither, and to call the Python version that you want explicitly in your scripts.

Upvotes: 1

Kevin
Kevin

Reputation: 76194

(This answer assumes you're using Windows, but I expect it mostly applies to other OSes too, modulo some details)

"python.exe" is the actual interpreter. You have one for each version of Python on your system. Which version gets executed when you run "python" on the command line, depends on your PATH environment variable and your current working directory.

"py.exe" is the Python Launcher. You probably only have one, even if you have multiple Python installs. Mine is in C:\Windows. It looks at the script, decides what version it's using, and delegates the actual execution to the proper interpreter.

Upvotes: 2

Related Questions