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 |
|---|---|---|
| Installing and Running Python | 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_test_env pythonYou can test this by running python in the command line.
Running Python in the Terminal¶
On Windows, open Miniforge Prompt. On a Mac or Linux machine, open Terminal.
Activate your conda environment:
$ conda activate pythia_test_envCreate a directory to store our work. Let’s call it
pythia_test.$ mkdir pythia_testGo into the directory:
$ cd pythia_testCreate a new Python file:
$ touch mysci.pyOn Windows:
type nul > mysci.pyAnd now that you’ve set up our workspace, edit the
mysci.pyscript using your favorite text editor (e.g., nano):$ nano mysci.pyOn Windows, a quick option is to edit on Notepad:
notepad mysci.pyChange 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 Notepad, Ctrl+S and close the window.
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.