Summary
Python programming will be much easier once you have a minimal understanding on how to use the terminal (the black box), the shell (the program the black box displays) and the prompt (the way the shell tells you it's your turn).
In Windows, it all starts with the lowest denominator: cmd.exe. You should know how to type a command, modify it, and search in the history using only the keyboard. The most important command being "cd" to go somewhere.
Come on, it's 2023, do I really need this?
There are ecosystems where you can code for a long time without having to touch a terminal, especially if you stay in the Microsoft sphere of influence.
That's not the case with Python. You will need it, because that's the only platform that works almost the same no matter what is your situation. And the only reliable mean to figure out what's going on when things don't work.
You don't need to become an expert, having the basics to not bleed time into typing commands is enough to be productive. You do need those basics though. This is one of those cases where spending an hour will save you days.
I'm not going to make the speech where I say with a deep voice and a wise wrinkly gaze that in time, you will come to appreciate it, and even gain from it.
While I daily drive a Linux machine and is quite fond of my little black square, I know there are plenty of people that really don't care for it. And frankly are happier if they can do the least possible with it.
That's fair.
With this article, I'm going to give you the minimal set of things you need to know to be able to use Python to its fullest capacity, while wasting the least time and energy battling the shell.
Terminals, shells and prompts
Before doing anything, let's clear off some confusion, by defining a few things.
People on the web use the words terminal, shell and prompt interchangeably, but they are not the same thing.
So when you see a Hollywood movie with a guy in a hoodie hacking "the 32 firewalls of the pentagon" in 7 seconds with some mysterious white text in a black box, what is what?
What is a terminal?
A terminal emulator, or just terminal for short, is a program that pretends to be this:
It's a very basic program that takes some input, pass it to another program, get back a response, and displays it on the screen.
And it waits until it gets some new input, and does it all over again.
The terminal is extremely dumb, it takes things in, gets things out, but has no idea of what's happening.
Visually, the terminal is usually a big black box, often it displays white letters, but doesn't know what the letter means.
On Windows, you can start cmd.exe to see this terminal:
What is a shell?
While technically a shell is any computer program that exposes an operating system's services, in practice, nobody uses that definition anymore.
Today, a shell usually means a command-line interpreter, so a program that:
Accepts some order from the user in the form of a textual command.
Understands that order, and performs an action from it.
Gets back to the user with the result of that action.
The shell can't display anything, though, to do that, it must exist in a terminal that will pass the user input to it, and display the shell output.
This is why people confuse "shell" and "terminal", because the main use case is to have a shell and a terminal working together. In fact, when you start a terminal, it almost always starts a shell immediately.
I open cmd.exe and immediately type "dir", I'll get this:
The program that understands "dir", does something with it, and gets back to me with an answer is the shell.
What is a prompt?
When a shell expects a user to type something, it stops, and tells the terminal to display a short text followed a blinking cursor.
This is the prompt.
It's the way the shell says it's waiting for you to play your turn:
Different shells have different prompts.
Lies, Damn lies and CLI tool names
On modern Macs, the default shell is called Z shell (zsh), and the default terminal is called Terminal.app.
Easy.
On Ubuntu, the default shell is called Bourne Again SHell (bash), and the default terminal is called...err... gnome-shell.
(mumble something about cache invalidation)
On Windows the default shell is called - oh come on! - Command Prompt.
And the default terminal? Command Prompt as well.
Turns out cmd.exe is both its own terminal and shell.
It's like they want you to fail at this.
Hello world
On Windows, the easiest and most reliable way to start a terminal is to click on the "Start" menu, type "cmd" and press "enter".
This will start the default Windows terminal.
Make sure you don't start something called "powershell". We want to start "Command Prompt".
The terminal should display a prompt, waiting for you to enter something.
Let's make it say hello. Type.
echo hello
Then press enter.
You already did all the following:
Started a terminal.
Started a shell.
Ran a command in the shell.
A shell in a shell
You just started the default Windows terminal, which started the default shell.
This shell doesn't understand Python.
Now, if you have installed Python in the way we recommended, you have the "py launcher" at your disposal.
Type:
py
This will displays this:
>>>
This is because you just started a Python shell, and ">>>" is the default prompt of the Python shell.
Now you can type (then enter):
print("hello")
Because the Python shell understands Python.
However, the Python shell doesn't understand "echo":
If I then type "exit()", I will be back into the default windows shell.
At this point your probably understand the gist of it:
A terminal starts with a default shell.
But you can start another shell in the same terminal.
If you exit this new shell, you go back to the previous shell.
More importantly, each shell only understands its own commands, not the commands of the other shells.
This last point is important.
If you work with Python, and somebody tells you to type "import os", you should do it in a Python shell, because it's Python code. Only the Python shell understands Python code.
But if somebody tells you to type "py -3.8 -m venv .venv", even if you are creating a Python virtual environment with this command, you are doing so using a command from the default Windows shell. Only this shell understands this command. If you run it in the Python shell, it will not work.
This is a mistake I see very often with beginners: people entering commands made for the system shell in the Python shell, and vice versa.
In my tutorial, I'll make sure to always tell you were to type what.
But if when you read the rest of the web, be sure to be vigilant about this.
You are here
There is a deeply important concept in any shell: you are currently somewhere.
When you enter a command, this command is executed where you are, meaning in the directory you are.
On windows, by default, it is displayed in the prompt. Remember? This guy:
I am in the C:\Users\Brennan directory.
This is extremely important, because any action you take is made in the context of this directory.
E.G., if I do:
DEL rick_roll.mp3
This will delete the file "rick_roll.mp3" inside "C:\Users\Brennan" and nowhere else.
Even in the Python shell, you are in a directory:
>>> import os
>>> os.getcwd()
'/home/user'
Here, I am in the "/home/user" directory.
Are we there yet?
You can move using the command "cd".
E.G., if I want to move to "C:\Users", I will do
cd "C:\Users"
The easiest way to get the path to a directory is to open the file explorer and go above the directory you want. Then HOLD SHIFT, and right-click on the directory icon. You will get a secret menu entry called "copy as path" that will put the path to this folder into your clipboard.
You can then paste it in the terminal.
Very useful, especially for some tricky folders like "Desktop" or "Documents".
A few tricks
You can't click to move the cursor. Use the keyboard left/right arrows, and the "home" and "end" key.
If you stumble upon something called "powershell", close it, and go back to "Command Prompt". Powershell is a good product, but the permission system it comes with will make your life difficult.
Don't type the same command twice. Use the top and bottom arrows of your keyboard to navigate in your command history and get back the command you want.
Copy/pasting sucks in cmd.exe. Ctrl+C/V or right click won't work as you expect.
About the latter point:
To copy something, hold "left-click" and select what you want to copy. Release "left-click", then immedialty "right-click". Not a joke.
To paste, something, make sure you are not selecting anything. Now right-click. Done.
There are terminals with much better ergonomics, but before window 11, you have to install them yourself. In order to make this tutorial as useful as possible to the maximum of persons and contexts, we stick to cmd.exe.
I may do a tutorial in the future about better terminals, and how to use them.
But you should not wait for it. Even if cmd.exe is uncomfortable, start using it.
Nice intro. The terminal-shell-prompt distinction was quite understandable for a beginner like me!