A Python script is a file containing Python code, usually with a .py extension. It can contain anything from a simple one-line command to a complex application. Running these scripts allows you to execute your Python code outside of an interactive environment, making it possible to automate tasks, run complex programs, and share your code with others.

How to run a python script

Creating a Script

To create a Python script, you need a text editor. Let's start by creating a simple one-line command in our text editor (e.g., VS Code):

print("We are running this script in the terminal!")

Saving the Script

Now we are going to save that script. There are a few rules and best practices to consider. These not only ensure that your script works correctly but also help maintain clarity and organization in your codebase.

File Extenstion
For Python scripts, always use the .py extension. This identifies the file as a Python script and allows the Python interpreter to recognize and run it. For example, my_script.py.

Avoid Spaces
Do not use spaces in script names. Spaces can cause issues in the command line and require extra handling (like quoting the file name). Use underscores (_) or hyphens (-) instead. For example, data_analysis.py or data-analysis.py.

Begin With a Letter
Start script names with a letter. Avoid starting with numbers or special characters. For example, script1.py is acceptable, but 1script.py is not recommended.

Case Sensitivity
Remember that Unix-like systems are case-sensitive. So, script.py and Script.py would be considered different files. It's a common practice to use lowercase with underscores for readability and to avoid confusion.

Avoid Reserved Words
Do not use names that are reserved words in Python or that might conflict with standard command names in Unix-like systems. For instance, avoid names like test.py or select.py, as they might clash with existing system commands or Python library modules.

Length and Descriptiveness
Keep the name reasonably short while still being descriptive of what the script does. For example, backup_database.py is clear and concise.

Special Characters
Avoid using special characters like !, @, #, $, %, etc., in script names. These characters could have specific meanings in the command line and can lead to unexpected behavior.

Use Dashes for Command-Line Tools
If the script is intended to be used as a command-line tool, it's common to use dashes instead of underscores. For example, data-cleaner.py.

I will save my script as practice.py and store it on my desktop. It's crucial to remember the directory where you save your script because we'll need to navigate to that location to run the script later.

Running the Script

To run your Python script from the terminal, follow these steps:

Open the Terminal: Open your terminal or command line interface.

Navigate to the Directory: Use the cd command to navigate to the directory where your script is saved. For example, if practice.py is saved on your desktop, you might use a command like:

Last login: Mon May 20 15:11:59 on ttys005
username@MacBook-Pro ~ % cd Desktop

Execute the Script: Once you are in the correct directory, run the script using the Python interpreter. Depending on your system configuration and Python version, you might use either python or python3 followed by the script name. For example:

Last login: Mon May 20 15:11:59 on ttys005
username@MacBook-Pro ~ % python practice.py

or

Last login: Mon May 20 15:11:59 on ttys005
username@MacBook-Pro ~ % python3 practice.py