Home Interests Python

How to Run Python in Terminal

run python in terminal
You can run any Python script in a command-line interface.
howchoo   (467)
August 18, 2023
6 minutes

Share

Interests
Posted in these interests:
python • 18 guides

Python and associated Python scripts can be run using command-line interfaces. Windows users can use command prompt while Mac and Linux users can make use of Terminal. We’ll cover how to run a Python script, open a Python shell, and how to run a Python one-liner.

1 – Run a Python script in Windows

run a python script in windows

Open Command Prompt. An easy way to reach Command Prompt is by opening the Start Menu and searching for cmd. Select Command Prompt from the list of applications.

How to run a Python script

By default, you will need to point Command Prompt to the Python installation location. Here’s an example of what a command would look like when executing a Python script.

C:\Python27\python.exe C:\Users\YourUsernameHere\Desktop\script.py

If you don’t want to type the full Python installation path, you can add an exception to your PATH environment variable for python.exe.

Using the Python shell

From the Command Prompt window, type python or python3 and press enter.

Python one-liners

This will as long as Python has been added to your PATH environment variable. Here’s an example of a one-liner you can run from command prompt.

python -c "print('hello world')"

2 – Run a Python Script on a Mac or Linux

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')"
NEXT UP

Class Vs. Instance Variables in Python 3

howchoo
howchoo   (467)
November 22, 2023

When learning object oriented programming in Python, there can be a few gotchas when it comes to distinguishing between class and instance variables. In this guide I’ll explain the difference between class and instance variables and provide examples demonstrating various use cases. 1 – Class vs. instance variables First, a quick review if you’re new

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore