As a Python programmer working with Linux, you might have encountered a situation where you need to run your Python scripts in the terminal. It can be a daunting task, especially if you’re new to the Linux environment. However, with this guide, you’ll be able to execute your Python scripts on Linux like a pro.
My guide provides a step-by-step approach that will walk you through the process of running Python scripts on Linux, command by command. With this guide, you’ll be able to navigate the Linux terminal with ease and run your Python scripts effortlessly.
1. Understanding the Linux terminal
The Linux terminal is a command-line interface that allows you to interact with your computer. Unlike the graphical user interface (GUI) you might be familiar with, the terminal uses text-based commands. Once you get the hang of it, you might just prefer the terminal over the GUI!
General Syntax: $ command [options] [arguments]
Output:
$ echo "Hello, World!"
Hello, World!
2. Checking if Python is installed
Before we dive deep into running scripts, let’s ensure you’ve got Python installed.
General Syntax: $ python --version
or $ python3 --version
Output:
$ python3 --version
Python 3.8.5
If you don’t see a version, it means you might need to install Python. However, most Linux systems come with Python pre-installed these days.
3. Making your Python script executable
Here’s a thing I learned the hard way: before running your Python script, it’s good practice to make it executable.
General Syntax: $ chmod +x script_name.py
This command gives your script the permission to execute. The ‘+x’ tells Linux that this file is executable.
Output: No output is a good sign here. It means the command executed successfully without any hitches!
4. Running your Python script
Here comes the fun part! Once your script is executable, you can run it in the terminal.
General Syntax: $ ./script_name.py
Output: Let’s say you have a script hello.py
that prints “Hello from the terminal!”
$ ./hello.py
Hello from the terminal!
If you haven’t made your script executable (from the previous step), you can also use this alternative:
General Syntax: $ python3 script_name.py
Output:
$ python3 hello.py
Hello from the terminal!
5. Using shebang for specifying interpreter
Ever stumbled upon #!/usr/bin/env python3
at the beginning of a Python script? That’s called a shebang. It’s not just some magic incantation! It tells the system which interpreter to use for running the script.
Including this at the top of your script is a great practice, especially if you’re sharing your scripts with others. It ensures everyone runs the script with the correct interpreter.
Honestly, I’ve overlooked the shebang line multiple times and later wondered why my scripts weren’t behaving as expected. Learn from my mistakes!
6. Managing Python packages
Ah, the world of Python packages! Once you get into Python, there’s a good chance you’ll use packages. To manage these, you’ll need pip.
To check if pip is installed:
General Syntax: $ pip3 --version
Output:
$ pip3 --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
To install a package:
General Syntax: $ pip3 install package_name
Output:
$ pip3 install requests Collecting requests Downloading requests-2.24.0-py2.py3-none-any.whl (61 kB) ... Successfully installed requests-2.24.0
Some handy tips
- Tab Autocomplete: One of the features I absolutely adore in the Linux terminal. Start typing a command or filename and press
Tab
to autocomplete. It’s a genuine time-saver! - Command History: Want to see the commands you’ve used in the past? Simply hit the
Up
arrow key. Alternatively,history
command can show you a list. - Clearing the terminal: Too much clutter?
clear
command to the rescue.
Essential Python-related commands in the Linux terminal
Command | Description |
---|---|
python3 --version |
Check the installed Python 3 version. |
chmod +x script_name.py |
Make a Python script executable. |
./script_name.py |
Execute an executable Python script. |
python3 script_name.py |
Run a Python script using Python 3 interpreter. |
pip3 --version |
Check the installed pip3 version (for managing Python packages). |
pip3 install package_name |
Install a Python package using pip3. |
pip3 uninstall package_name |
Uninstall a Python package using pip3. |
pip3 freeze |
List all installed Python packages with their versions. |
pip3 show package_name |
Show detailed information about a specific installed package. |
python3 -m venv venv_name |
Create a virtual environment named “venv_name”. Virtual environments are isolated Python environments. |
source venv_name/bin/activate |
Activate a virtual environment. After activation, your terminal will use the Python & pip of this venv. |
deactivate |
Deactivate the virtual environment, returning to the system’s Python version. |
Frequently Asked Questions (FAQs)
Q1: Can I run Python 2 scripts in the same way as Python 3 scripts in the Linux terminal?
Answer: Yes, you can, but there are some key differences to note. For example, to check the version for Python 2, you’d use python --version
(not python3
). Similarly, to run a script, you’d use python script_name.py
. However, Python 2 is no longer officially supported since 2020, so it’s recommended to transition to Python 3.
Q2: What if I don’t have pip3
installed on my Linux system?
Answer: You can easily install it using sudo apt install python3-pip
on Debian-based systems or sudo yum install python3-pip
on Red Hat-based systems. Once installed, you can manage Python packages using pip3
as discussed.
Q3: I get a “Permission Denied” error when trying to run my Python script. Why?
Answer: This typically means your script doesn’t have execute permissions. Remember to use chmod +x script_name.py
to make it executable.
Q4: Why do some Python scripts have #!/usr/bin/env python3
at the top?
Answer: This is called a shebang. It indicates which interpreter should be used to run the script. By using #!/usr/bin/env python3
, you’re specifying that the script should be run using Python 3.
Q5: Can I run Python scripts without making them executable?
Answer: Absolutely! Just use python3 script_name.py
to run the script. Making them executable simply provides a shorthand method to run them with ./script_name.py
.
Q6: How can I stop a running Python script in the terminal?
Answer: You can generally stop a running script by pressing Ctrl + C
in the terminal. This sends an interrupt signal, causing the script to terminate.
Q7: What if I want to run a Python script in the background?
Answer: You can use the &
symbol after your run command, like ./script_name.py &
. This will run the script in the background, freeing up your terminal.
Conclusion
Navigating the Linux terminal to run Python scripts can initially feel overwhelming. However, with the right guidance, it can become an intuitive process. This guide has walked you through the essentials of running Python scripts in the terminal, including checking if Python is installed, making scripts executable, executing them directly in the terminal, and managing Python packages with pip. Moreover, the provided table offers a quick reference for common Python-related commands in the terminal, and the FAQ answers most typical questions regarding the topic at hand. As with everything in the tech world, the more you practice and get familiar with these processes, the easier they will become.