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.
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 hello.py
This will open the
Vi Editor Commands
First, let’s look at the commands you can use in the vi editor.
|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|
|→ + ||Move Cursor Faster|
|$||Move to end of line|
Write Your Python Script
To write in the
vim editor, press
i to switch to
Write the best python script in the world.
esc to leave the editing mode.
Write the command
:wqto save and quite the vim editor (
Run Your Script
Run your script by typing python
hello.py in the Terminal.
Congratulations, you now know how to create and run a Python script using the Command-line.
Sr SEO Specialist at Seek (Melbourne, Australia). Specialized in technical SEO. In a quest to programmatic SEO for large organizations through the use of Python, R and machine learning.