You log in to a server, type python, and get “command not found”. Or you run a script and it picks up a different Python than you expected. Or someone hands you a .py file and you’re not sure whether to run it, import it or just make it executable. All of it comes from the same few basics, and they take maybe twenty minutes to sort out.
This post walks through them in order: which Python you have and where it lives, three ways to run code, what the shebang line does, and a small system info script you can keep. Every command and output below came from real test machines running Ubuntu 26.04.1 LTS and AlmaLinux 10.2, so what you see is what you should get on Ubuntu and RHEL-family systems. If you haven’t set up Python yet, my Python for DevOps Engineers post covers install options first.
Understand Python Versions, Interpreter Paths and Compatibility
Start with the quick check. Ask the machine which Python 3 it has and where that comes from:
python3 --version
which -a python3
readlink -f "$(which python3)"
Ubuntu 26.04.1 LTS:
Python 3.14.4
/usr/bin/python3
/bin/python3
/usr/bin/python3.14
AlmaLinux 10.2:
Python 3.12.15
/usr/bin/python3
/bin/python3
/usr/bin/python3.12
Two things to notice. First, the versions are different. Same command, same OS family on paper, different Python. Second, /usr/bin/python3 is really a link to a versioned file like python3.14. That’s how distros let several versions live side by side.
/bin/python3 shows up as well because on modern Ubuntu and RHEL systems /bin is just a link to /usr/bin. Same file, two doors.
Why the plain “python” command behaves differently
Try the short name on both machines. Ubuntu says python: command not found. AlmaLinux happily runs Python 3.12.15, because it ships /usr/bin/python as a link to python3. On Ubuntu you’d install the python-is-python3 package to get that link.
That’s why scripts should ask for python3 by name and never rely on bare python.
Ask Python where it’s running from
Sometimes which isn’t enough, especially with virtual environments around. Python can tell you itself:
python3 -c "import sys; print(sys.executable); print(sys.version_info[:3])"
/usr/bin/python3
(3, 14, 4)
The AlmaLinux output was the same shape, with (3, 12, 15). Inside a virtual environment, sys.executable points into the venv folder instead, which is a quick way to confirm you activated the right one.
Compatibility in plain words
You’ll bump into version gaps. Here’s what actually matters when you write scripts for a mixed fleet:
- Python 3.12 and 3.14 both run ordinary scripts fine. f-strings,
subprocess,pathlibandjsonbehave the same on both. - Newer syntax is the trap. If you use something added in 3.13 or 3.14, it fails on the 3.12 box with a
SyntaxError. - Stick to the standard library for admin scripts. No installs, no version surprises.
- Don’t swap the system Python. Tools like
dnfrun on it. Add another version beside it instead.
A good habit is to write on the oldest Python you support, then test on the newest.
Run Python Scripts, Modules and the Interactive Shell
There are three everyday ways to run Python, and you’ll use all of them in a week.
1. The interactive shell
Type python3 with nothing after it. You get a prompt where each line runs right away. It’s the fastest way to test a thought:
python3
Python 3.12.15 (main, Oct 1 2026, 00:00:00) [GCC 14.3.1 20251022 (Red Hat 14.3.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.cpu_count()
22
>>> 3 * 7
21
>>> exit()
That’s AlmaLinux. Ubuntu looks the same apart from the version banner. Leave with exit() or Ctrl+D.
2. One-liners with -c
The -c flag runs a string of code and quits. Handy in a pipeline or a quick check:
python3 -c "print(2**10)"
1024
3. Modules with -m
This one gets overlooked. -m runs a module that Python already ships with, and some of them are genuinely useful tools. json.tool pretty prints JSON, so you don’t need jq for simple jobs:
echo '{"service":"nginx","port":80}' | python3 -m json.tool
{
"service": "nginx",
"port": 80
}
Both distros gave identical output. Another favorite is python3 -m http.server, which serves the current folder over HTTP. Perfect for grabbing a file from one box to another in a lab.
4. Script files
Anything you’d type in the shell can go in a file ending in .py, and you run it with python3 filename.py. That works, but you can make it nicer. That’s where the shebang comes in.
Python Shebang and Executable Scripts on Linux Explained
The shebang is the first line of a script, starting with #!. It tells Linux which program should run the file. Without it you have to type python3 in front every time. With it, and the right permission, you can just run the file like any other command.
Make a small test file called hello.py:
#!/usr/bin/env python3
import sys
print(f"Hello from Python {sys.version_info.major}.{sys.version_info.minor}")
Now try running it as is:
./hello.py
bash: ./hello.py: Permission denied
Right. The file isn’t executable yet. The shebang does nothing until you flip that bit:
chmod +x hello.py
./hello.py
ls -l hello.py
Ubuntu:
Hello from Python 3.14
-rwxr-xr-x 1 asif asif 112 Oct 16 08:08 hello.py
AlmaLinux printed Hello from Python 3.12. Same file, no edits, and each machine used its own Python. That’s the whole point.
env python3 or /usr/bin/python3?
You’ll see two common styles:
#!/usr/bin/env python3looks through yourPATHand picks the firstpython3it finds. It follows an activated virtual environment, which is usually what you want.#!/usr/bin/python3always uses the system Python, whatever your environment says. System tools likednfdo this on purpose.
For your own scripts, go with env. For something that must always use the distro Python, hardcode the path. Also, you can ask for a specific version, like #!/usr/bin/env python3.12, if a script really needs it.
The Windows line ending problem
This one bites people who edit scripts on a laptop and copy them over. If the file has Windows line endings, Linux reads the interpreter name as python3 followed by a carriage return. I added one on purpose to show the error. On Ubuntu:
env: 'python3\r': No such file or directory
AlmaLinux shows the same thing as /usr/bin/env: 'python3\r': No such file or directory. If you ever see a strange \r in that message, convert the file with sed -i 's/\r$//' hello.py and it’s fixed. Also remember that the shebang has to be the very first line. Not second, not after a blank line.
Write Your First Python System Information Script
Time to put it together. This script prints a short snapshot of a machine: OS, architecture, Python version, CPU, memory, disk and uptime. It uses only the standard library, and it works the same on Ubuntu and RHEL-based systems. Save it as sysinfo.py:
#!/usr/bin/env python3
"""sysinfo.py - print a quick snapshot of the machine. Standard library only."""
import os
import platform
import shutil
def os_pretty_name():
# /etc/os-release exists on Ubuntu, RHEL, AlmaLinux and most other distros
with open("/etc/os-release") as f:
for line in f:
if line.startswith("PRETTY_NAME="):
return line.split("=", 1)[1].strip().strip('"')
return "unknown"
def mem_gb():
info = {}
with open("/proc/meminfo") as f:
for line in f:
key, value = line.split(":", 1)
info[key] = int(value.split()[0]) # values are in kB
return info["MemTotal"] / 1024 / 1024, info["MemAvailable"] / 1024 / 1024
def uptime_hours():
with open("/proc/uptime") as f:
return float(f.read().split()[0]) / 3600
total, free = mem_gb()
disk = shutil.disk_usage("/")
print(f"Hostname : {platform.node()}")
print(f"OS : {os_pretty_name()}")
print(f"Arch : {platform.machine()}")
print(f"Python : {platform.python_version()}")
print(f"CPU cores: {os.cpu_count()}")
print(f"Memory : {free:.1f} GB free of {total:.1f} GB")
print(f"Disk / : {disk.used / 1024**3:.1f} GB used of {disk.total / 1024**3:.1f} GB")
print(f"Uptime : {uptime_hours():.1f} hours")
Quick tour of what’s happening:
platformandosgive you the hostname, architecture, Python version and CPU count.shutil.disk_usage("/")returns total, used and free bytes for the root filesystem.- Memory and uptime come from
/proc, which is just text Linux keeps up to date. Reading a file is all it takes. - The functions at the top keep each job small, so you can reuse them later.
Make it executable and run it:
chmod +x sysinfo.py
./sysinfo.py
Ubuntu 26.04.1 LTS:
Hostname : ubuntu-lab
OS : Ubuntu 26.04.1 LTS
Arch : x86_64
Python : 3.14.4
CPU cores: 22
Memory : 13.8 GB free of 15.3 GB
Disk / : 4.3 GB used of 1006.9 GB
Uptime : 24.8 hours
AlmaLinux 10.2:
Hostname : alma-lab
OS : AlmaLinux 10.2 (Lavender Lion)
Arch : x86_64
Python : 3.12.15
CPU cores: 22
Memory : 13.8 GB free of 15.3 GB
Disk / : 2.6 GB used of 1006.9 GB
Uptime : 24.8 hours
One script, two distros, no changes. Why not psutil? It’s great, but it needs pip, and installing packages into the system Python is something your distro actively discourages. For a first script, the standard library is the calmer choice.
Ideas to extend it
- Add the load average with
os.getloadavg(). - Warn when disk usage goes past 85 percent, then run it from cron.
- Print the result as JSON with the
jsonmodule, so a monitoring tool can read it.
Confirm Everything Works
Run these three checks on any new server and you’ll know your Python basics are in place:
python3 --version
head -1 sysinfo.py
./sysinfo.py | head -3
Python 3.14.4
#!/usr/bin/env python3
Hostname : ubuntu-lab
OS : Ubuntu 26.04.1 LTS
Arch : x86_64
That’s Ubuntu. On AlmaLinux the version line reads Python 3.12.15 and the OS line shows AlmaLinux 10.2. If the last command runs without a “Permission denied” or “bad interpreter” message, the interpreter, shebang and permissions are all fine. There’s no web page here, so terminal output is the proof.
Frequently Asked Questions
What does #!/usr/bin/env python3 mean?
It’s a shebang line. It tells Linux to run the file using env, which finds python3 in your PATH. That keeps the script working on machines where Python lives in different places.
Why do I get “Permission denied” when I run my script?
The file doesn’t have the executable bit. Run chmod +x script.py and try again, or skip the permission and run python3 script.py.
How do I check which Python version my script will use?
Run which -a python3 to see the candidates, and python3 -c "import sys; print(sys.executable)" to see the one that actually runs. A virtual environment changes the answer.
Why does “python” not work on Ubuntu but works on AlmaLinux?
Ubuntu doesn’t ship the bare python command by default. Install python-is-python3 if you want it, or just use python3 everywhere. AlmaLinux includes a python link to python3.
Is it safe to change the default python3 on a server?
No. System tools such as dnf depend on the version the distro shipped. Install another version next to it and call it by name, for example python3.13.
Where to Go Next
Try adding one feature to sysinfo.py and run it on a real server this week. Once that feels easy, read Python vs Bash for DevOps automation to decide which tool fits your next job. And if shell basics still feel shaky, your first Bash script and the shebang on Linux Pathfinder covers the same idea from the shell side.