1
0
mirror of https://github.com/tiyn/wiki.git synced 2026-07-26 04:21:34 +02:00

Python: Added Shebang Usage

This commit is contained in:
2026-07-15 14:11:44 +02:00
parent c116d3a433
commit 99083a2c02
3 changed files with 70 additions and 1 deletions

View File

@@ -35,6 +35,44 @@ There are similar repositories and resources for functions that are outside the
shells. shells.
Because of that and their issue regarding compatibility they will not be mentioned here. Because of that and their issue regarding compatibility they will not be mentioned here.
### Shebangs
A shebang specifies which interpreter should execute a script.
It is written as the first line of the file and starts with `#!`.
For example, a [POSIX shell](#unix-shells) script typically begins with the following.
```sh
#!/bin/sh
```
A [Bash](#unix-shells) script commonly uses the following line.
```sh
#!/usr/bin/env bash
```
Using `/usr/bin/env` causes the interpreter to be resolved through the current `PATH` instead of
using a fixed path.
This makes scripts more portable and allows them to use interpreters provided by virtual
environments—for example
[Pythons virtual environments](/wiki/programming_language/python.md#using-virtual-environments)—or
other tools.
To execute a script directly, it first has to be marked as executable.
```sh
chmod +x <script>
```
It can then be started directly.
```sh
./<script>
```
The operating system will automatically invoke the interpreter specified by the shebang.
### Symbolic Links using `ln` ### Symbolic Links using `ln`
`ln` is a command to create links between files and folders. `ln` is a command to create links between files and folders.

View File

@@ -88,6 +88,23 @@ python3.9 --version
This section addresses the usage of Python. This section addresses the usage of Python.
### Executable Python Scripts
Python scripts can be made directly executable without explicitly calling the Python executable
itself every time.
For general information about shebangs, executable scripts and the `chmod +x` command, refer to the
corresponding section in the [Shell article](/wiki/linux/shell.md#shebangs).
For Python it is recommended to use the following shebang.
```py
#!/usr/bin/env python
```
Using `/usr/bin/env python` makes the script compatible with
[virtual environments](#using-virtual-environments), [uv](/wiki/programming_language/python/uv.md)
projects and other Python environment managers.
### Convert `.ipynb` Files to `.py` Files and Back ### Convert `.ipynb` Files to `.py` Files and Back
Jupyter notebooks in the `.ipynb` format can easily be converted to normal Python files using Jupyter notebooks in the `.ipynb` format can easily be converted to normal Python files using

View File

@@ -8,12 +8,26 @@ dependencies.
## Setup ## Setup
Refer to the [official installation guide](https://docs.astral.sh/uv/getting-started/installation/) Refer to the [official installation guide](https://docs.astral.sh/uv/getting-started/installation/)
to install `uv` on your operating system. to install uv on your operating system.
## Usage ## Usage
This section addresses the usage of uv. This section addresses the usage of uv.
### Executable Scripts
uv fully supports Python scripts that use the standard Python shebang.
```py
#!/usr/bin/env python
```
This allows scripts to automatically use the Python interpreter provided by the current uv project
or [virtual environment](/wiki/programming_language/python.md#using-virtual-environments).
For a general explanation of shebangs and executable scripts, refer to the
[Shell article](/wiki/linux/shell.md#shebangs).
### Managing Python Versions ### Managing Python Versions
Python versions can be installed and pinned for the current project by replacing Python versions can be installed and pinned for the current project by replacing