Create and Run a Python Script Using the Command-line Only

Share this post

This post is part of the complete Guide on Python for SEO

Integrated Development Environment’s like VSCode are super useful because they take care of most of the difficulties around programming. But, what if you don’t want to use an IDE to work with Python, but use command-line instead?

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

If you don’t know what is the command-line, this post is probably too advanced for you. Just read the complete guide on Python for SEO.

Getting Started

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:


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.

Create the Python File

We will use vim to create a new Python file. Vim is a text editor that you can use from the command-line.

$ vim

This will open the vim editor.

Vi Editor Commands

First, let’s look at the commands you can use in the vi editor.

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

Write Your Python Script

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

You Might Also Like  SEO Split Test Using Python + CausalImpact + Tag Manager

Write the best python script in the world.

Press esc to leave the editing mode.

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

Run Your Script

Run your script by typing python in the Terminal.

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