MirkoK >
Unix and its
descendants (Linux, macOS, etc.) don’t use file extensions (like
.py, .png, etc.) to associate a file-type with an application.
Dear MirkoK,
I have a vague recollection of how the Macintosh actually used a four character word in the Resource fork for how to determine which program a file belonged to. (Long time ago now. Macintosh SE era.)
And, much more recently I had a problem on my Linux box getting Python and Pingus files to open with the correct program. I think I have that problem worked out now.
Not to mention that I am now a much happier camper now that I have that hash bang and the permissions ironed out and I can make the scripts run as expected.
I intend to continue doing most of my ‘work’ on this Windoze 7 box. But, if my grandsons want to try to learn computer programming at least I have a ‘safe place’ to turn them loose with it.
Thanks again, Josh.
| MirkoK
October 1 |
The shebang, shell-bang or hash-bang is a line at the beginning of
an executable text file (ie. a script) which tells Unix and
Unix-like operating systems how to execute the file. Unix and its
descendants (Linux, macOS, etc.) don’t use file extensions (like
.py, .png, etc.) to associate a file-type with an application.
Since there are many scripting languages, the OS needs a way to know
with which programming language interpreter that file should be
executed.
If a file “program.py” contains the hash-bang “#!/usr/bin/python”
then the operating system (the kernel) runs “/usr/bin/python
program.py”. The same is true if the program is simply called
“program” (without an extension).
Without the hash-bang, the kernel runs the file with the default
interpreter “/bin/sh” which is an entirely different scripting
language interpreter for shell scripts.
You’ve said that your Linux system is “Linux Mint 20.3”. What would
be more useful is to know the file-manager which you use? Nautilus?
I’m on Linux Mint 22.2 and am not sure if there are any difference.
On LM 22.2 Nautilus isn’t well configured to run Python scripts
directly, you have to setup this yourself:
- Go to “$HOME/.local/share/applications/” where “$HOME” is your
user directory (like “/home/josh”) and create a file
“python3.desktop” with the following content:
[Desktop Entry]
Version=1.0
Name=Python 3
Comment=Run with Python 3
Keywords=programming;code;development;python;
Exec=python3 %F
Icon=python
Terminal=true
Type=Application
StartupNotify=true
Categories=GTK;GNOME;Development;
MimeType=text/x-python;application/x-python;
-
Go back to your file-manager, right-click on some Python script
and choose “Open with”.
-
Find the just created “Python 3” entry. If it is not there, log
out and login again.
-
Select that “Python 3” entry. At the bottom of the window, you
should see a checkbox (switch) “Always use for this file type” and
below that the file-type “Python-3-Script”. Enable this
checkbox/switch and click “Open”.
After this, double-clicking a Python3 Script should always run the
script directly instead of opening it in a text-editor or ask you,
what to do.