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.
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.
- 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
- 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
- Switch to Insert Mode
Modify the Python file by switching to Insert Mode by Pressing the letter “i”.
- Write Your Code in the Vim Editor
Once in insert mode, you can start writing Python code in the Vim editor.
- Write and Quit Your Vim Editor (:wq)
Press
ESC
and then use the:wq
command to save the file, then pressENTER
.
Vi Editor Commands
First, let’s look at the commands you can use in the vi editor to create your python file.
i | Switch to Insert mode (editing mode) |
esc | Exit the editing mode |
dd | Delete the current line |
u | Undo the last change |
:q! | Close the editor without saving changes. |
:wq | Save the text and close the editor |
→ + Shift | Move 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
- Navigate to directory
- Use the vim filename.py command
- Switch to insert mode
- Write your Python code
- Write and quit the vim editor
- Run Your .py file
Just read the complete guide on Python for SEO.

SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.