As a full-stack developer and Linux expert, I often need to run Python scripts on Linux systems. Python is one of the most popular programming languages used by developers today. Its simplicity yet powerful capabilities make it well-suited for scripting on Linux and other Unix-based operating systems.
In this comprehensive guide, I will walk you through the various methods of running Python scripts on Linux. Whether you are just getting started with Python development or are a seasoned coder looking to improve your workflow, this guide has got you covered.
An Overview of Python
Before we dive into the details of running scripts, let‘s do a quick recap of what Python is and why it has become so ubiquitous.
Python is a high-level, general-purpose programming language that emphasizes code readability with its clear and concise syntax. Guido van Rossum created it in the early 1990s. The key characteristics that have contributed to Python‘s widespread popularity are:
-
Interpreted: Python code is executed line-by-line by an interpreter rather than being compiled. This allows for rapid prototyping and development.
-
Dynamically typed: Variables in Python do not require explicit declaration of their data type. The interpreter infers types during runtime automatically.
-
Object-oriented: Python supports object-oriented programming, allowing developers to organize and reuse code efficiently.
-
Batteries included: The Python standard library is vast, with modules for everything from web development to scientific computing.
-
Cross-platform: Python code can run unchanged on various operating systems like Linux, Windows, and macOS.
-
Open source: Python has a large community contributing to its development as an open-source project. Libraries and tools are abundantly available.
These characteristics make Python a favorite of developers, scientists, data analysts, machine learning engineers, and system administrators alike. Let‘s look at how we can leverage it by running scripts on Linux.
Creating a Sample Python Script
Before running a Python program, we first need to create one. While complete Python projects are organized as packages containing modules and multiple files, the most basic Python program can simply go into a single .py
file.
Let‘s create a simple "Hello, World!" script for demonstration purposes. Open up a terminal and follow along:
$ nano hello.py
This will open up the nano text editor and create an empty file named hello.py
. Now enter the following two lines of code:
#!/usr/bin/env python3
print("Hello, World!")
That‘s it! This is a valid Python script that prints a friendly greeting. Save the file (Ctrl+O
) and close nano (Ctrl+X
).
Making the Script Executable
By default, a newly created Python file does not have execute permissions. We need to explicitly grant that by using chmod
:
$ chmod +x hello.py
The +x
flag adds execution rights to the file for the current user. We can verify by checking the permissions as follows:
$ ls -l hello.py
-rwxrwxr-x 1 user user 39 Feb 11 22:19 hello.py
Now our script is ready to be executed!
Running Python Scripts
There are a few different ways to run Python code on Linux. Which method you use depends on your specific needs and preferences. Let‘s go through them one-by-one:
Using the python command
The simplest way is to run the script is by calling the python
command and passing the script file as an argument:
$ python hello.py
Hello World!
When we run this command, the Python interpreter executes the code in hello.py
line-by-line, thereby printing out the friendly greeting.
Note that some Linux distributions have both Python 2 and Python 3 installed. It‘s recommended to explicitly call python3
to run your code with Python 3 rather than the outdated Python 2.
Using ./
The current directory is not included in the operating system‘s PATH
variable by default. So we need to specify the path explicitly when executing our script.
An easy way is to use ./
before the script name – this tells the system to look for and execute the file named hello.py
in the current directory.
$ ./hello.py
Hello, World!
This approach comes in handy when you want to execute a script by just its filename rather than typing out absolute or relative paths.
AddingExecute Rights to Directories
Having to type ./
before script names can get tedious. We can configure Linux to scan certain directories for executables when you run commands.
The PATH
environment variable contains a list of such directories that are checked sequentially when you try to execute a program. We can add the current directory (denoted as .
) to PATH
as follows:
$ export PATH=$PATH:.
Now you‘ll be able to run the hello.py
script simply as:
$ hello.py
Hello, World!
This sets PATH
only for your current shell session. You can add this export line to your shell startup script (e.g. .bashrc
) to retain it when starting new shells.
Calling Interpreter Magic Comments
So far we have been explicitly calling python
or python3
interpreters to run the scripts. Python also provides a "magic comment" at the top of the file to mark the required interpreter.
For example, to indicate that Python 3 should be used, insert the following as the first line of the script:
#!/usr/bin/env python3
The env lookup finds where the python3
command lives and uses that interpreter.
We‘ll need to grant execute rights to our script for this approach to work:
$ chmod +x hello.py
$ ./hello.py
Hello, World!
Nowadays, Python 3 is the de facto standard version for development. But you can change python3 to python2 if working on legacy systems.
Creating Standalone Executables
So far, we have been running Python code using an interpreter. This means the target system needs to have Python installed. What if you wanted to distribute self-contained executables of your script?
Tools like PyInstaller can bundle your Python program along with all its dependencies into a single standalone binary. Users can simply download it and run – no need for installing interpreters or libraries beforehand!
Here is how we would use PyInstaller to package our previous script:
$ pip install pyinstaller
$ pyinstaller --onefile hello.py
$ ls dist
hello
$ ./dist/hello
Hello, World!
One key benefit of this approach is obscuring source code, which might be desirable for commercial products.
Going Beyond with Automation
The methods described above are great for running Python scripts manually as a developer or power user. What about executing them automatically as part of larger workflows?
The Linux cron scheduler along with helper tools like Ansible, Salt, and Chef allow you to orchestrate Python programs as:
- Scheduled jobs that kick off scripts at certain times, dates, or intervals
- Provisioning and configuration management workflows across servers
- Deployment pipelines to roll out applications
- Asynchronous workers processing queues
- Anything else that needs automation!
With cron, you can very easily schedule a Python script to be invoked periodically. For example, editing the user crontab with crontab -e
allows you to enter:
*/5 * * * * python /path/to/script.py
This will cause the script.py
program to be executed every 5 minutes by the cron daemon!
For advanced automation, Ansible is a fantastic platform that enables executing Python programs and playbooks across nodes over SSH. That brings immense power when trying to coordinate fleets of Linux machines.
The sky is truly the limit when it comes to creative ways for triggering Python code – on Linux or otherwise!
Embedding Python in Shell Scripts
While we have covered running Python files directly, sometimes it is convenient to embed Python code snippets within traditional shell scripts.
Bash does not support functions or complex data structures which can make scripting tedious. Python provides these features – so combining it with Bash gives the best of both worlds!
Here is a simple example of a Bash script (script.sh
) with some Python:
#!/bin/bash
# Use Python print function
python -c ‘print("Hello from Python!")‘
# Define and call a Python function
python -c ‘
def greet(name):
print(f"Hello {name}")
greet("Reader!")‘
The -c
flag tells Python to execute the inline code that follows. When we run the Bash script, it will invoke the Python interpreter to evaluate the snippets:
$ ./script.sh
Hello from Python!
Hello Reader!
This allows tapping into Python where you need it without having to write separate programs.
Going Further with IPython
For even more power and convenience when working with Python interactively on Linux, I highly recommend installing IPython.
IPython provides a robust REPL shell with autocompletion, syntax highlighting, debugging, and many other features. It is perfect for testing code snippets, analyzing data, and working on the command-line productively.
Of course, you also get the full power of the Python language in the IPython shell. It understands all valid Python syntax which can be executed directly:
$ ipython
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type ‘?‘ for help.
In [1]: print("Hello from IPython!")
Hello from IPython!
In [2]: import math
...: math.factorial(5)
...:
Out[2]: 120
In [3]: exit
$
I highly recommend spending time getting comfortable with IPython if you write Python code regularly on Linux systems. It will boost your productivity tremendously!
Conclusion
In this comprehensive guide, we explored several methods for running Python scripts and code snippets on Linux:
- Creating executable
.py
files and invoking them with interpreters - Marking scripts with magic comments for direct execution
- Bundling Python programs into standalone binaries
- Scheduling and automation with cron
- Embedding Python alongside Bash using
-c
Whether you are executing short code samples or full-fledged applications, Python offers immense power and flexibility. Mastering the techniques above will enhance your ability to build, deploy, and run Python effectively as a Linux user.
The key is to understand the basics of how Python execution works on an operating system. Don‘t be afraid to experiment with the various approaches highlighted here. Python was designed to make life easier for developers and power users alike.
With this foundation, you are now ready to start architecting and automating your workflows with the might of Python on Linux! The sky is the limit when it comes to what you can achieve.