![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
Stop copy‑pasting the same command every time you run Python. This guide will teach the practical ways to run Python scripts no matter which operating system, editor, or automatic scheduler you use.
It’s any plain text file with Python code inside, typically saved with the`.py` extension.
For this tutorial, we’re going to use a password generator script. This script also includes the Python module `random`. A Python module is a file containing Python code (functions, classes, variables) that can be imported and reused in other Python programs. A Python module is meant to be imported and reused, while a script is intended to be executed directly to perform a specific task.
Open Terminal and type the following command to run the password generator script:
If you want to make it executable and run it like an app, follow these steps:
2. Run the script directly:
Python installs with a launcher called `py`. Use it to run your script:
Alternatively, if Python is set in your PATH, you can run the following:
If you get errors like `’python’ is not recognized`, rerun the installer and check “Add Python to PATH,” or just use the `py` launcher.
Passing command-line arguments lets users provide input without changing the original code. It promotes a few Pythonic principles by making your script more flexible and modular. Passing in command-line arguments allows one script to handle different tasks or settings efficiently.
You’ll need to update the original password generator code file to include the argparse module. In addition to importing `argparse`, we are going to add a function to parse input and pass the `length` argument into` generate_password()` in the `__main__` block.
If you’re running the script on macOS or Linux, you’ll also need to add a shebang (e.g., `#!/usr/bin/env python3`) to the top of your file. This line tells the operating system what interpreter to use when executing the script directly.
Now we’re ready to go!
2. Run it with the argument:
Windows doesn’t use the shebang (`#!/usr/bin/env python3`) like Unix systems do. But you can still run the script from Command Prompt or PowerShell using `python` or `python3`.
Open Command Prompt (or PowerShell):
Press `Win + R`, type `cmd` or `powershell`, and hit Enter.
2. Run the script now with a custom length:
Running a script in an IDE gives you helpful tools like autocompletion, debugging, and visual feedback. It’s great for development. It’s a more comprehensive option than using the command line, especially for more complex projects.
PyCharm is a powerful Python IDE with smart editing, debugging, and project tools that streamline development. It excels at code completion, debugging, and integration with version control and virtual environments.
VS Code is faster, lighter, and more flexible than PyCharm — perfect for multilanguage projects and custom setups.
Jupyter Notebook isn’t a traditional IDE like VS Code or PyCharm. It stands out for interactive, cell-based execution and real-time visualization, all in one document. Jupyter is great for data analysis but also runs regular Python scripts.
Jupyter Notebooks are my preferred way to build tutorials. I use VS Code when I build applications.
There are many ways to run Python in Jupyter, but here’s one example. This example focuses on building and running the code inside the Jupyter notebook. It doesn’t use any files that are saved elsewhere.
We’ll need to adjust our `password_generator.py` code before it works in Jupyter.
After clicking the run button ( ▶ in the upper left), the script will ask for a password length. Enter the number, press enter, and it will generate your password.
`cron` is great for recurring tasks and for running scripts at specific times.
2. Add a line to run your script daily at 3:00 AM:
You can log the output with the following code:
`systemd` is a modern alternative to `cron` that ensures scripts run even after reboots. The following runs the script daily at 3 a.m.
Create a new timer file:
Enable:
Errors are part of the process. If you run into errors while executing your Python script, here are some quick troubleshooting tips to help you get back on track.
ModuleNotFoundError
Make sure your virtual environment is activated:
Permission denied (Unix)
Make the script executable with `chmod +x script.py`, or run with `python3 script.py`.
PATH confusion
Use absolute paths (`/usr/bin/python3`, `C:\Projects\password_generator.py`) to avoid environment issues.
Here are some best practice tips you can implement once you’re running scripts smoothly.
Isolate dependencies using a virtual environment.
Freeze dependencies to lock in versions.
Avoid unintended execution when importing by guarding the entry point.
Bundle scripts with PyInstaller for systems without Python.
Now that you know how to run Python scripts across macOS, Windows, and Linux, you’ve got everything you need to take your ideas from concept to execution no matter the platform.