guagay_wk
guagay_wk

Reputation: 28030

Proper shebang of python script running on Windows

Python scripts running on Linux machines have this shebang on top.

#!/usr/bin/python

For Windows, what is the proper shebang to use on top? I am using Windows 10.

Upvotes: 7

Views: 10169

Answers (3)

waithira
waithira

Reputation: 340

Using a python extension in VSCode or any other IDE ,set the path to your interpreter as the first line of code Windows 10.python 3.9

#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe 
print('hello world')

Upvotes: 1

cowlinator
cowlinator

Reputation: 8794

For Windows, the proper shebang to use is actually the same:

#!/usr/bin/python

Python uses "virtual shebangs", which allows certain specific "Unix-style" shebang lines to be portable between Unix and Windows.

On Windows, you need to either associate .py files with any Python executable, or run python scripts using "The Python Launcher" (the py.exe command).

Once you associate .py files with any Python executable (or use py.exe), you can execute python files directly by simply typing myscript.py or double clicking it in Windows Explorer. Then, the virtual shebang will be used to execute the script with the designated Python executable.

Upvotes: 3

Anthon
Anthon

Reputation: 76607

The interpretation of the shebang lines on Linux (and Unix like machines) is done by the operating system, and Windows doesn't do that, so there is no proper shebang line.

So if you don't want to do something special like selecting a particular Python version for your program, you can leave out the shebang (or leave in the one you need for running on Linux).

If your .py files are registered on Windows to be started by a particular Python executable, it is possible to check the first line of that file and interpret it to make sure you start the right version.

It is possible to start another Python version in that way with the original file and other arguments, but keep in mind that if you use that to start e.g a Python 2.7 interpreter where Python 3.8 is the one registered, that your Python program must be valid Python for both versions (so no print statements, f'{somevar}' strings, etc.)

Upvotes: 2

Related Questions