How to Run Python in Terminal

run python in terminal

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

Class Vs. Instance Variables in Python 3

howchoo

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

In these interests

Python python • 18 guides

Share this guide!

howchoo

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.

Related to this guide:

howchoo

Class Vs. Instance Variables in Python 3

When learning object oriented programming in Python, there can be a few gotchas when it comes to dis

In these interests: CodePython

howchoo

Getting Started with Django Testing

I have been programming for a while and have only recently begun to implement testing into my develo

In these interests: DjangoPython

Automatically Control Your Raspberry Pi Fan (and Temperature) with Python

Since the Raspberry Pi 4 was released, many have noticed that it can get pretty hot, espec

In these interests: PythonRaspberry Pi

How to Control a DC Motor (Or Motors) Using Your Raspberry Pi

Controlling DC motors from your Raspberry Pi is quite easy! Whether you want to control a single mot

In these interests: PythonRaspberry Pi

howchoo

Combine Two Querysets in Django (With Different Models)

Today, I stumbled upon a use case where I needed to have a querysets that had objects from different

In these interests: DjangoPython

howchoo

Python time.sleep() – How to Make a Time Delay in Python

Learn how to use Python’s sleep function to make a timed delay. tl;dr 1 – Import the tim

In these interests: CodePython

howchoo

Use the Python range() Function to Generate Sequences of Numbers

In Python, range is an immutable sequence type, meaning it’s a class that generates

In these interests: Python

howchoo

Python Regexes – findall, search, and match

This guide will cover the basics of how to use three common regex functions in Python – findal

In these interests: PythonRegex

howchoo

How to Rerun a Django Migration

By default, Django migrations are run only once. But sometimes we need to rerun a Django migration,

In these interests: DjangoPython

How to Check Your Python Version

It’s good to know what version of Python you’re running. Sometimes you may need a specif