简体   繁体   中英

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.

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 eg 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.)

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')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM