You’d like to learn to run Python in the terminal. Here we will cover:
Installing Python in the terminal
Running Python code in the terminal
Time to learn: 20 minutes
If running Python in the terminal, it is best to install Miniconda. You can do that by following the instructions for you machine.
Then create a Conda environment with Python installed by typing the following into your terminal:
$ conda create --name pythia_foundations_env python
You can test this by running
python in the command line.
On Windows, open Anaconda Prompt. On a Mac or Linux machine, simply open Terminal.
Activate your Conda environment:
$ conda activate pythia_foundations_env
Create a directory to store our work. Let’s call it
$ mkdir pythia-foundations
Go into the directory:
$ cd pythia-foundations
Create a new Python file:
$ touch mysci.py
And now that you’ve set up our workspace, edit the
mysci.pyscript using your favorite text editor (nano, e.g.):
$ nano mysci.py
Change the script to include the classic first command: printing, “Hello, world!”.
Save your file and exit the navigator. How to do this is dependent on your chosen text editor.
In Vim the command is
In Nano it is Ctrl+O to save and Ctrl+X to exit (where you will be prompted if you want to save it, if modified).
In the terminal, execute your script:
$ python mysci.py
Congratulations! You have just set up your first Python environment and run your first Python script in the terminal.
Running Python in the Terminal is a good option if you are familiar with Linux commands or scripting on a super computer. It requires the use of a text editor.