Python in the Terminal
Overview
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
Prerequisites
Concepts |
Importance |
Notes |
---|---|---|
Helpful |
Time to learn: 20 minutes
Installing Python in the Terminal
If you are running Python in the terminal, it is best to install Miniforge. You can do that by following the instructions for your 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.
Running Python in the Terminal
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
pythia-foundations
.$ 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.py
script using your favorite text editor (e.g., nano):$ nano mysci.py
Change the script to include the classic first command: printing, “Hello, world!”.
print("Hello, world!")
Save your file and exit the editor. How to do this is dependent on your chosen text editor.
In Vim, revert to command mode by pressing
esc
. Then, the command is:wq
.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.
Summary
Running Python in the terminal is a good option if you are familiar with Linux commands or scripting on a supercomputer. It requires the use of a text editor.