How to Set Up a Python Programming Environment
Table of Contents
- Introduction
- Install Miniconda
- Install VS Code
- Complete the Python Refresher Assignment
- Do this once for every course
- Do this everytime you work on a course
Introduction
This tutorial aims to:
- help you set up a Python programming environment for coding assignments, research, or working with with RSOs
- have you use your programming environment to complete a Python refresher assignment
Some of steps are based on the Visual Studio Code Python tutorial.
How to open a terminal
When we say “open a terminal,” what we mean is to start the Terminal application. Here is one way to do that:
- Click the Launchpad icon in the Dock.
- Type “Terminal” in the search field.
- Click Terminal.
See documentation on Open Terminal for more information. Note that it is often helpful to have more than one terminal window open at the same time (or more than one tab in the same window).
How to run a command
When we say “run a command,” what we mean is to type something into the terminal window and press return. For example, suppose we said:
run the command
pwd
to find your current working directoryYou would type
pwd
into the terminal window and press return, with the result being something like this:(base) ➜ ~ pwd /Users/httran
See documentation on Execute commands and run tools in Terminal on Mac for more information. Also see the command-line basics or Command Line Primer page for a list of frequently used commands.
Install Miniconda
Conda is a package management system that includes Python. We suggest you install Miniconda to install conda and Python.
Install Xcode Command Line Tools
Open a terminal and run the following command, accepting all default options. You may be asked to restart your computer during or after this process. Please do so if prompted.
xcode-select --install
You may get a message like following. If so, command line tools are already installed, and you can skip to Install Miniconda.
xcode-select: note: Command line tools are already installed. Use "Software Update" in System Settings or the softwareupdate command line interface to install updates
Install Miniconda
-
Check if conda is already installed by trying to update it:
conda update -n base conda
If this process succeeds, then conda is installed, and you can skip to Verify Installation. If this process does not succeed, continue with the installation below.
-
Go to the Miniconda installer page and download the appropriate
.pkg
file for your Mac architecture. See the Mac computers with Apple silicon page for help determining whether you have Apple silicon or Intel Mac. -
Double-click the downloaded
.pkg
file and accept all the default options during installation. -
After installation, restart your terminal (close and open the terminal).
Verify Installation
Open a new terminal and run:
conda --version
You should see the conda version number if the installation was successful.
Install VS Code
We suggest installing Visual Studio Code (VS Code) to use as your integrated development environment (IDE) for writing Python code.
Install VS Code
Follow the instructions in the Install VS Code on macOS page to download and install VS Code.
Remember to drag the Visual Studio Code.app file into your Applications folder.
Install the Python and Jupyter Extensions
- Open the VS Code application.
- Click the Extensions icon in the Activity Bar in on the left-hand side of VS Code.
- Install the Python extension.
- Type “Python” into the search bar.
- Click the Python extension and click the Install button in the window that appears.
- Install the Jupyter extension.
- Type “Jupyter” into the search bar.
- Click the Jupyter extension and click the Install button in the window that appears.
Complete the Python Refresher Assignment
You can now use your VS Code workspace and Conda environment to complete the python-refresher.ipynb
file found here. If the link shows a raw JSON data, right click the link, and select “Save Link As” to download the file.
Follow the steps in the Do this once for every course section to set up an environment for the python-refresher.ipynb
assignment. Be sure to replace instances of “my-course” with “computing-readiness” and “my-assignment” with “python-refresher.”
Follow the steps in the Do this every time you work on a course section to complete the assignment.
Do this once for every course
You should follow these steps once for every course (and assignment).
Create a folder for the course and assignment
You should organize your code files into folders (or directories) based on courses and assignments. For example, you might store files for this tutorial in a computing-readiness
folder within a projects
folder (where you store all coursework) within your Documents
folder, organized like this:
├── Documents
│ ├── personal
│ ├── projects
│ ├── ae202
│ ├── ae370
│ ├── computing-readiness
│ ├── python-refresher.ipynb
How to change the working directory
All the files on your computer are organized in folders, which are commonly referred to as “directories.” When you are working on the command line in a terminal, you are working in one of these directories. Commands you run can find files in that directory, but cannot (by default) find files in other directories.
When we say “change the working directory,” we mean exactly that — telling the terminal the directory in which you want to work.
To do this, we run the command
cd path/to/directory
where
path/to/directory
is replaced by the location of the directory in which you want to work. One easy way to find this location (i.e., the “path” to your directory) is by dragging its folder from the Finder into your terminal window (see documentation on Drag items into a Terminal window on Mac). In particular, I would first typecd
(note the single trailing space):(base) ➜ ~ cd
Then, I would drag a folder into the terminal window and press return. For instance, suppose I had created a folder called
computing-readiness
somewhere on my computer and dragged it in, then pressed return — I would see something like this:(base) ➜ ~ cd /Users/httran/Documents/projects/ae-computing/python-refresher (base) ➜ python-refresher
I could then use the
pwd
command to print the working directory, which would show something like this:(base) ➜ python-refresher pwd /Users/httran/Documents/projects/ae-computing/python-refresher
See documentation in the Specify files and folders in Terminal on Mac page for other ways to specify the path to a directory.
You can create the `computing-readiness’ folder and structure shown above by opening a terminal and running the following commands. Note that lines starting with a “#” symbol are comments, not commands to enter.
# change your working directory to your home directory
cd
# show the folders within your home directory (optional)
ls
# change your working directory to the Documents folder
cd Documents
# make a projects folder (if needed)
mkdir projects
# change your working directory to your projects folder
cd projects
# make a "my-course" folder
mkdir my-course
# change your working directory to the "my-course" folder
cd my-course
# make a "my-assignment" folder
mkdir my-assignment
# change your working directory to the "my-assignment" folder
cd my-assignment
Create a conda environment for the course
You should create and use a separate conda environment for each course.
VS Code provides a good explanation for why: “A best practice among Python developers is to use a project-specific virtual environment. Once you activate that environment, any packages you then install are isolated from other environments, including the global interpreter environment, reducing many complications that can arise from conflicting package versions.”
Create a conda environment for your course (replacing “my-course” with your course name) by opening a terminal and doing the following:
-
Create a conda environment named “my-course”.
conda create --name my-course
Your command prompt should show something like the following:
Channels: - defaults Platform: osx-arm64 Collecting package metadata (repodata.json): done Solving environment: done ## Package Plan ## environment location: /Users/httran/anaconda3/envs/my-course Proceed ([y]/n)?
Enter
y
to proceed. -
Active your
my-course
conda environment. Any packages you install with conda will now be installed in this conda environment.conda activate my-course
-
Install Python 3 and Jupyter in your conda environment (install Python 3.11 because Jupyter only supports up to 3.11).
conda install python=3.11
Enter
y
to proceed.conda install jupyter
Enter
y
to proceed.
A Conda cheatsheet of common Conda commands can be found here.
Do this everytime you work on a course
You should follow these steps every day before you start to work on a course or assignment.
- Open a terminal.
-
Change your working directory to your assignment directory. For example, assuming the directory structure shown in Create a folder for the course and assignment, you could run the following commands:
cd cd Documents/projects/my-course/my-assignment
-
Activate your conda environment by running this command:
conda activate my-course
You should see the prefix to your terminal prompt change from
(base)
to(my-course)
. This means you are in the conda environment you created for your course.As an alternative, you can also select your conda environment to use while working in VS Code by doing the following:
- Open the Command Palette by going to VS Code’s View > Command Palette
- Search for “Python: Select Interpreter” and select the command.
- Select the conda environment you just created and installed Python in “Python 3.13.1 (‘computing-readiness’) … Conda”.
-
Open VS Code to work on your assignment by running this command:
code .
As an alternative to VS Code, you can also work with Jupyter notebooks in a browser by running this command:
jupyter notebook
A browser window should open with the Jupyter notebook interface. You can now navigate to and open any Jupyter notebooks (with extension
.ipynb
).
We strongly recommend you duplicate and work with a copy of any given notebook rather than working with the original. Feel free to ignore this suggestion if you are a git
expert.