We often use the command line or "shell" -- a text-based interface to your operating system -- to run Python scripts and perform other tasks. You can learn how to open a terminal (another name for the "shell") by doing a web search for "How to open a terminal on Mac" (or Windows if that's your operating system).
To use the shell, you'll also want to get familiar with some command-line basics.
First, open a terminal. If you need help on that front, see How do I use a command-line shell (aka "terminal")? in this FAQ.
Then, in the shell, just type python
or, on systems, you may need to type python3
. That should drop you into an interactive coding environment where you can execute snippets of Python code. Here's an example session:
# type either "python" or "python3" (it varies by system)
~> python
Python 3.11.4 (main, Jul 26 2023, 20:55:49) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> total = 2 + 2
>>> print(total)
The chevrons (>>>
) above indicate visually that you are in an interactive Python session. On these lines, you can type normal Python code and hit return
to execute the code on a given line.
To close the shell and drop back to the regular terminal, press Control + D
or type exit()
and hit return
.
NOTE: Any work you do in an interactive shell will be lost after you close it, so be sure to copy it into a normal text file if you need to save the work.
Note, depending on how your shell is set up, you may need to place the below environment variable in a different configuration file such as
~/.bash_profile
,~/.bashrc
.
- Open the Terminal
- Paste the following command:
echo "PS1='\w> '" >> ~/.profile
- Hit enter
- Close the Terminal and re-open a new Terminal
You should see a prompt that looks like this: ~>
Open Terminal.
echo "export BASH_SILENCE_DEPRECATION_WARNING=1" >> ~/.profile
Close and re-open Terminal
There are a few options, but one easy way is to create a shell alias in the appropriate configuration file.
Depending on how your system is set up, this file wil vary but typically it will be
~/.profile
or~/.bash_profile
for Mac users in this class, or~/.bashrc
for Linux users.
You'll need to locate the Visual Studio Code program on your system, and use that to set up the alias.
For example, on my machine:
# e.g. in ~/.profile
alias vscode="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
Once set, start a new terminal. Then you should be able to do the following on the command line:
# This should open up a new file called awesome_new_scripy.py in VS Code
vscode awesome_new_script.py
Some students reported warnings about "malicious" software when trying to install VSCode or Atom code editors on Mac Catalina.
This StackOverflow post has a temporary work-around. Probably worth trying the simpler solution first (System Preferences > Security & Privacy
).
Mac users who have upgraded to Catalina reported that they don't have basic shell utilities such as ls
, cp
, etc. in the Terminal.
Running xcode-select --install
appears to fix the problem.
Note: This is one of the steps you need to run in our technical setup, so please make sure to only run it once.
Typically, you'll want the latest "LTS" release from the Ubuntu download page.
How do I edit hidden files on a Mac?
Mac and Linux systems contain hidden files and directories that typically contain configuratons for various programs on the system. An example is ~/.bashrc
, which contains configurations for the bash shell.
These files can be opened and edited with a standard code editor such as Visual Studio Code or Atom.
For this example, we assume you have Visual Studio Code installed.
In order to edit such files, you should open a Finder window.
Hit command
+ shift
+ .
to make invisible files and folders appear in the Finder.
Use the Finder menu to navigate to your Home directory using Go -> Home
.
You should now see your home directory, which contains a number of hidden files and directories starting with a .
. For this example, let's assume we want to edit a hidden configuration file for DataKit.
Navigate to the ~/.datakit/plugins/datakit-github
folder.
Right-click on config.json
.
You should get a pop-up menu with options for opening the file using different programs.
Choose Open With
and select a code editor such as Visual Studio Code or Atom to open and edit the file.
If you wish to once again hide these .
-prefixed files and directories, hit command
+ shift
+ .
once again in the Finder window.