How to Create and Run a Python Script with Terminal (Command Line Example)

Read this post if you want to learn how to create Python files from terminal.

A Python file (or script) is a file written in the Python Programming language ending with the .py extension.

I will show you how you can create and run a Python script using the command-line only.


Subscribe to my Newsletter


Getting Started

Integrated Development Environment’s like VSCode are super useful because they take care of most of the difficulties around programming. But, what if you want to create a Python file from the command-line (Terminal) instead of using an IDE?

Open the Terminal.

First, make sure that you have Python installed on your computer.

$ which python

If Python is installed, you’ll have something like this:

/usr/bin/python

Write Simple Code Using the Python Interpreter

If you just want to write simple code like print('Hello world!'), you can use the python interpreter by typing python in the Terminal.

The python interpreter lets you write and execute code line by line.

As you write more complex code, you will want to pre-write your code in .py files. To do this without leaving the Terminal, you will need to use the vim editor to pre-write them.

How to Create a Python File in Terminal

You can create a Python file by typing “vim” along with the file name in the Terminal. For example, you can create a new Python file called “hello.py” by typing “vim hello.py” in the terminal. This will open a new file in Vim where you can start writing your Python code.

Steps to create Python file in the Terminal.

  1. Navigate to the directory where you want to create your Python File

    Use the “cd” command to navigate to the directory where you want to create your Python File

    cd into your directory

  2. Create a new Python file using the “vim” command

    Create a new Python file called “hello.py” by typing “vim hello.py” in the terminal

    create python file with vim

  3. Switch to Insert Mode

    Modify the Python file by switching to Insert Mode by Pressing the letter “i”.

    switch to insert mode in the vim editor

  4. Write Your Code in the Vim Editor

    Once in insert mode, you can start writing Python code in the Vim editor.

    write python code in the vim editor

  5. Write and Quit Your Vim Editor (:wq)

    Press ESC and then use the :wq command to save the file, then press ENTER.

    write quite vim editor

Vi Editor Commands

First, let’s look at the commands you can use in the vi editor to create your python file.

iSwitch to Insert mode (editing mode)
escExit the editing mode
ddDelete the current line
uUndo the last change
:q!Close the editor without saving changes.
:wqSave the text and close the editor
→ + ShiftMove Cursor Faster
$Move to end of line

Open The VIM Editor

You can also choose an alternative text editor such as Nano.

$ vim hello.py

This will open the vim editor.

Write Your Python Script

To write in the vim editor, press i to switch to insert mode.

Write the best python script in the world.

Press esc to leave the editing mode.

Write the command :wq to save and quite the vim editor (w for write and q for quit).

How to Run the Python Script in the Terminal

Run your script by typing python hello.py in the Terminal.

You Have Learned How to Make a New Python File in Terminal

Congratulations, you now know how to create and run a Python script using the Command-line.

Let’s recap how to create Python Files in the Terminal

  1. Navigate to directory
  2. Use the vim filename.py command
  3. Switch to insert mode
  4. Write your Python code
  5. Write and quit the vim editor
  6. Run Your .py file

Just read the complete guide on Python for SEO.

4.5/5 - (10 votes)