Mastering the Execution of Python Scripts from Any Directory on MacOS
Written on
Chapter 1: Introduction
Do you wish to execute a Python script from any location on your Macbook? If so, you’re in the right place. Let me guide you through the process.
With this technique, I’ve created a personal assistant on my Macbook that can:
- Access specific work folders in Finder, VSCode, or Terminal
- SSH into my AWS virtual machines
- Launch JupyterLab without needing to type python -m jupyterlab
- Organize certain folders, like screenshots, with ease
All this functionality can be triggered using a single command (I use lzl).
What’s Under the Hood
At the core of this process is a Python script stored on my computer. For instance, let’s assume it’s located at /Users/lzl/Documents/main. This script performs tasks, such as opening specified folders in VSCode.
By creating an alias, I can execute this Python script from any directory on my Macbook.
Creating a Useful Python Script
Here’s a simple example of a Python script:
# /Users/lzl/Documents/main/run.py
print('Good morning')
This example prints "Good morning," but you can modify it to perform any useful function.
Choosing a Simple Command
Next, we want to run this script from anywhere using an easy command. Let’s use haha for simplicity. This means that executing haha in the terminal will print "Good morning."
Setting Up the Alias
First, we need to open the ~/.zshrc file, where our aliases will be stored.
Note: The file name is .zshrc, located in your home directory.
- Open a terminal instance (press Command + Space, type "Terminal," then hit Enter).
- Change the directory to your home folder by typing cd ~.
- Use code . to open this directory in Visual Studio Code, or opt for vim or nano if you prefer those editors.
Editing the .zshrc File
Once you have the .zshrc file open, add the following line:
alias haha='python3 /Users/lzl/Documents/main/run.py'
This line instructs your Macbook that typing haha triggers the command python3 /Users/lzl/Documents/main/run.py.
Refreshing the .zshrc File
After saving your changes, refresh the .zshrc file by executing the following command in the terminal:
source ~/.zshrc
This step ensures your new alias is now active.
Executing Your Alias
You can now run haha from any directory in the terminal, which will execute python3 /Users/lzl/Documents/main/run.py, printing "Good morning."
Feel free to select a more meaningful alias and enhance your Python script to perform valuable tasks!
If You Wish To Support Me As A Creator
- Applaud this article by giving it a clap
- Share your thoughts in the comments
- Highlight your favorite parts
Thank you! These small gestures mean a lot to me!
Chapter 2: Video Tutorials
To further assist you, here are some helpful video resources.
How to run Python Scripts in Terminal on MacOS
This video provides a detailed guide on executing Python scripts using the terminal on MacOS.
How To Run Python Files From Terminal on Mac (2024)
This video will walk you through the latest methods for running Python files from the terminal on your Mac.