How To Run Python Program In Linux

How To Run Python Program In Linux

Reading time1 min
#Python#Linux#Programming#Python3#Virtualenv#Debugging

Mastering Python Execution in Linux: From Script Setup to Debugging Essentials

Launching Python on Linux is not a naive exercise in typing python script.py—production environments demand much more discipline. Inconsistent interpreter selection, unmanaged dependencies, broken file permissions, and subtle debugging hurdles are responsible for a majority of avoidable defects in Python/Linux workflows.


The Realities of Python Execution on Linux

Why does the interpreter sometimes ignore your latest code changes? Why does a script work for you, but fail in CI or on someone else’s system? These aren’t theoretical annoyances—they’re the byproducts of neglecting environment isolation, misconfigured shebangs, and permissions. Reproducibility in Python means consistency across shells, nodes, and distributions.


1. Script Setup: Shebang, Encoding, and Permissions

Minimal Python script with robust portability:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def main():
    print("Running under Linux. Expect utf-8 everywhere.")

if __name__ == "__main__":
    main()
  • The shebang line (#!/usr/bin/env python3) ensures the script uses whichever python3 is first in PATH, not some hardcoded location.
  • Explicit encoding guards you against errant editor defaults.

Permissions:
Set execution permissions before anything else—or your script won’t run directly:

chmod 755 ./myscript.py

755 is customary for team-shared scripts, though for personal use 700 suffices.

Gotcha:
If you see /usr/bin/env: ‘python3\r’: No such file or directory, you almost certainly have DOS line endings (CRLF). Never edit Linux scripts on Windows without proper conversion.


2. Python Interpreter: Selecting and Verifying Versions

Multiple interpreters are a fact of life on modern Linux distros. On RHEL 7, python is 2.7; on Ubuntu 22.04, python3 is 3.10 by default.

Check interpreters:

$ which python
/usr/bin/python

$ which python3
/usr/bin/python3

$ python3 --version
Python 3.10.12

Non-obvious tip:
On minimal Docker images (Alpine, Ubuntu core), neither python nor python3 may be present. Use package managers (apk add python3, apt-get install python3) and always verify the absolute path in automation scripts.


3. Virtual Environments: Isolation Not Optional

With multiple projects or automation tasks, system-wide Python packages cause non-deterministic failures.
A local virtual environment mitigates platform drift.

python3 -m venv .venv
source .venv/bin/activate

Now, pip install only contaminates .venv/lib/python3.10/site-packages, not the OS-wide site-packages.

Deactivate with:

deactivate

Not perfect:
venv is adequate for most local and CI tasks, but for deterministic builds (especially for deployment targets) consider pip-tools or Docker builds with explicit requirements.txt pinning.


4. Execution Patterns: Direct & Via Interpreter

Direct execution (shebang relies on a valid interpreter):

./myscript.py --arg1 value
  • Fails if myscript.py isn’t in PATH or lacks execute permission.
  • Logs will record only the parent process (e.g., bash shell) unless you arrange explicit logging.

Interpreter invocation (bypassing shebang):

python3 myscript.py --debug
  • Guarantees interpreter choice.
  • Preferred in automated jobs (cron, systemd, containers).

Arguments:
Handled with sys.argv or argparse. Watch for silent failures—misplaced options are ignored without strict parsing.


5. Debugging: Tracebacks, Logging, and Breakpoints

Common error:

Traceback (most recent call last):
  File "./myscript.py", line 7, in <module>
    main()
  File "./myscript.py", line 4, in main
    print("Value:", 1/0)
ZeroDivisionError: division by zero

Never suppress tracebacks. Logging to /dev/null or redirecting stderr hides useful failure diagnostics.

Logging best practices:

import logging
logging.basicConfig(level=logging.INFO)
logging.info("Started execution at %s", __file__)

For more subtle issues, use Python’s built-in debugger:

python3 -m pdb myscript.py

Python 3.7+ breakpoint (pauses execution):

breakpoint()
  • Insert anywhere—development equivalents of console.log.

6. Practical Notes and Non-Obvious Pitfalls

  • Line Endings: Convert with dos2unix myscript.py. Non-Unix line endings still break shebang parsing.
  • File Ownership: Scripts written on network filesystems may have permission inconsistencies. Verify with ls -l.
  • Environment Variables: Avoid hard-coding secrets—load via os.environ and support .env files with python-dotenv when needed.
  • PATH/PYTHONPATH: Scripts not found? Current directory might not be in PATH; PYTHONPATH collisions yield unpredictable imports.
  • Shebang Portability: #!/usr/bin/env python3 is safe on almost all Linux distributions since 2014.
    Do not use #!/usr/bin/python3 unless you control all target environments.

Example: Real-World Minimal Deployment Workflow

Suppose you need to ship myscript.py as a team automation tool on Ubuntu 22.04.

  1. Check interpreter with python3 --version. If not 3.10+, install with sudo apt install python3.
  2. Create a virtual environment: python3 -m venv venv-automation.
  3. Set permissions: chmod 755 myscript.py.
  4. Add any dependencies to requirements.txt.
  5. Activate env and install:
    source venv-automation/bin/activate && pip install -r requirements.txt
  6. Run script through the venv for a predictable context:
    ./myscript.py (for daily runs) or python3 myscript.py --batch (for cron jobs).
  7. Capture logs for incident analysis: redirect output and error as needed.

Conclusion

Reliable Python execution on Linux is won through attention to shebang consistency, interpreter explicitness, environment isolation, and careful debugging discipline. These principles scale—from ad hoc scripts to multi-node production deployments. Critical missteps—permissions, line endings, variant interpreters—account for real operational outages.

Still catching edge cases? There’s always another layer: systemd service wrapping, pipx for self-contained CLI tools, or OS-level sandboxing. For most workflows, the mechanics above yield systemic reliability.