Mac users can run Python scripts using Terminal. Launch Terminal to begin.
There are two common ways to run a Python script from the command line. You can call the python
program directly, and pass the name of the script to execute. Or you can make the script executable, and call it directly.
Run a script using python
Running a script using the python
program is easy, and it's probably the method most people are familiar with. Simply call python
and pass the name of your script, like this:
python myscript.py
# or
python3 myscript.py
Making a Python script executable
If your Python script includes a “shebang” (#!/usr/bin/env python
or #!/usr/bin/env python3
). You can make your script executable, like this:
chmod +x myscript.py
and execute it like this:
./myscript.py
Using the Python shell
You can open a Python shell simply by typing python
or python3
into a Terminal window. Then you can run Python commands directly in the shell.
Python one-liners
You can also execute Python directly on the cli using the -c
option. Example:
python -c "print('hello world')"