Configuring your computer to use Python for scientific computing¶
In this lesson, you will set up a Python computing environment for scientific computing. We will install Anaconda with its associated package manager, conda
. It has become the de facto package manager/distribution for scientific use.
Downloading and installing Anaconda¶
If you already have Anaconda installed on your computer, you can skip to the next section to install node.js.
Downloading and installing Anaconda is simple.
- Go to the Anaconda homepage and download the graphical installer. Be sure to use the most up-to-date version of Python.
- Install Anaconda using the graphical installer.
- Follow the on-screen instructions for installation. While doing so, be sure that Anaconda is installed in your home directory, as is the default, not in root.
That's it! After you do that, you will have a functioning Python distribution.
Install node.js¶
node.js is a platform that enables you to run JavaScript outside of the browser. We will not use it directly, but it needs to be installed for some of the more sophisticated JupyterLab functionality. Install node.js by downloading the appropriate installer for your machine from the node.js website.
Package installations¶
Because Anaconda ships with many (over 100) packages. As a result, the web of dependencies among the packages can be unwieldy, and it is difficult to ensure that each reader of this book has compatible versions. To alleviate this, and because we do not need all of the packages that come with Anaconda, we will create an environment specific for use with this book. This environment contains only the packages we use in this book and their dependencies and is therefore easier to manage. The environment is specified in a YAML file, which you can download:
You may need to right-click the link to download the file.
There are two ways you can create the environment, on the command line of via the Anaconda Navigator. We generally prefer the command line, but find many students like using the Navigator.
Installation using the command line¶
conda
is a package manager for keeping all of your packages up-to-date and consistent with regards to dependencies. It has plenty of functionality beyond our basic usage, which you can learn more about by reading the docs. Here, we will use its command line interface to create the environment.
To access the command line, if you are using macOS, you can use the Terminal
application. It is typically in the /Applications/Utilities
folder. Otherwise, hit ⌘-space bar and type terminal
in the search box, and select the Terminal Application. For Windows, you can use PowerShell, which you can launch through the Start
menu. If you are using Linux, it's a good bet you already know how to navigate a terminal, so we will not give specific instructions for Linux.
Once on the command line, you need to navigate to the directory where you have saved the bi1x.yml
file. Let's say you save it in the directory bi1x
in your home directory. You can navigate to that directory by entering
cd ~/bi1x
on the command line. (The ~
symbol is a shortcut for your home directory.)
Now, to create the environment, execute the following.
conda env create -f "http://bi1x.caltech.edu/2025/bi1x.yml"
It will take a minute for two for the environment creation to complete.
Installation using Anaconda Navigator¶
To install using the Navigator, you will need to be sure to download the bi1x.yml file.
Anaconda has a GUI-based interface called Anaconda Navigator. If you're using macOS, this is available in your Applications
menu. If you are using Windows, you access it from the Start
menu. You can then launch Anaconda Navigator.
After launching the Navigator, click Environments
on the left menu panel. After clicking that, you will see a panel open immediately to the right of the left menu panel with a Search Environments
window at the top. At the bottom of that panel, click Import
. Select Local drive
, and find the bi1x.yml
file you downloaded. Then, click Import
. It will take a minute for two for the environment creation to complete.
Activating the environment¶
After the environment is created, you need to activate it. To do this from the command line, execute the following.
conda activate bi1x
If you are using the Anaconda Navigator, click Home
on the left navigation menu. At the top of the window, you will see two pull-down menus. Select All application
on bi1x
.
You will need to activate the environment every time you open a new terminal (or PowerShell) window or launch Anaconda Navigator. (If you are using the command line, you can have this happen automatically if you like by adding conda activate bi1x
to your configuration file, e.g., .bashrc
.)
Launching JupyterLab¶
You can launch JupyterLab is from the command line. On Windows, this is usually accessed through PowerShell and on macOS through Terminal. To launch JupyterLab, type the following on the command line (again, after you have done conda activate bi1x
).
jupyter lab
You can specify the browser you want using, for example,
jupyter lab --browser=firefox
Alternatively, you can use the Anaconda Navigator. Upon launching the Navigator, you should see an option to launch JupyterLab on the Home
screen. (Be sure that you select All applications
on bi1x
in the top pulldown menus.) After clicking Launch
for JupyterLab, a new browser window or tab will open with JupyterLab running.
Within the JupyterLab window, you will have the option to launch a notebook, a console, a terminal, or a text editor. As you work through this book, you will use notebooks almost exclusively.
Checking your distribution¶
Let's now run a quick test to make sure things are working properly. We will make a quick plot that requires some of the scientific libraries we will use in this book.
Launch a Jupyter notebook in JupyterLab. In the first cell (the box next to the [ ]:
prompt), paste the code below. To run the code, press Shift+Enter
while the cursor is active inside the cell. You should see a plot that looks like the one below. If you do, you have a functioning Python environment for scientific computing!
import numpy as np
import bokeh.io
import bokeh.plotting
bokeh.io.output_notebook()
# Generate plotting values
t = np.linspace(0, 2 * np.pi, 200)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
# Make the plot
p = bokeh.plotting.figure(frame_width=300, frame_height=300)
p.line(x, y, line_width=3, color="red")
source = bokeh.models.ColumnDataSource(
dict(x=[0], y=[0], text=["Bi 1x"])
)
p.text(
x="x",
y="y",
text="text",
source=source,
text_align="center",
text_font_size="18pt",
)
# Display
bokeh.io.show(p)
Computing environment¶
%load_ext watermark
%watermark -v -p numpy,bokeh,jupyterlab
Python implementation: CPython Python version : 3.12.5 IPython version : 8.27.0 numpy : 1.26.4 bokeh : 3.6.2 jupyterlab: 4.2.5