---
title: "Jupyter Notebook Tutorial: Setting Up Python & Jupyter Notebooks on macOS for OpenAI Exploration"
published: 2023-07-05T10:48:04.000-04:00
updated: 2024-06-18T06:37:15.000-04:00
excerpt: "Unlock the power of Python and OpenAI with our step-by-step tutorial on installing a Jupyter Notebook. 
"
tags: Python, Jupyter Notebooks, AI, OpenAI, #CTA-vector
authors: Matvey Arye
---

> **TimescaleDB is now Tiger Data.**

Are you ready to delve into the world of AI and ChatGPT? Jumping right in is the best way to get started. As the most commonly used programming language in this field, [Python](https://www.python.org/) pairs perfectly with Jupyter Notebooks, a powerful tool for running Python scripts.

In this Jupyter Notebook tutorial, we'll guide you through setting up these tools on your macOS system. We will be utilizing the following components:

-   [Pyenv](https://github.com/pyenv/pyenv): to manage multiple Python installations on your machine, allowing different Python versions for various projects.
-   [Virtualenv](https://github.com/pyenv/pyenv-virtualenv): to create isolated environments for your Python projects, ensuring independent installations of packages and dependencies.
-   [Jupyter Notebook:](https://jupyter.org/) an interactive computing environment that enables you to create and share documents containing live code, visualizations, explanatory text, and more.

## How to Install a Jupyter Notebook Using Python

Let's start this Jupyter Notebook tutorial by installing the necessary dependencies:

```Python
brew update
brew install pyenv
brew install pyenv-virtualenv
brew install npm  # JupyterLab requires npm
```

Next, ensure that your shell is correctly utilizing pyenv (assuming you're using Z shell—or Zsh; for other shells, see the [pyenv GitHub repo](https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv)):

```Python
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
echo 'if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi' >> ~/.zshrc
```

Afterward, open a new shell to apply the updates and install the latest Python 3:

```Python
pyenv install 3
pyenv global 3
```

Now, let's create a virtual environment for your project:

```Python
pyenv virtualenv 3 jupyter_env
pyenv activate jupyter_env
```

At any time, you can view your virtual environments by running:  

```Python
pyenv virtualenvs
```

Install Jupyter Notebook in your new environment:

```Python
pip install jupyterlab
jupyter lab build
```

Create a directory for your project:  

```Python
mkdir my_jupyter_project
cd my_jupyter_project
```

To install some dependencies, execute the following command:  

```Python
pip install openai python-dotenv
```

Launch Jupyter Lab:

```Python
Jupyter-lab
```

You're now ready to explore:

-   Our [tutorial](https://timescale.ghost.io/blog/postgresql-as-a-vector-database-create-store-and-query-openai-embeddings-with-pgvector/) on OpenAI embeddings.
-   The [OpenAI cookbooks](https://github.com/openai/openai-cookbook).

📺

****Editor's Note:**** [Head to our YouTube channel to learn how to connect your Timescale database from Jupyter Notebooks](https://youtu.be/e4WilTU2E2Y).

## Extra, Extra! Storing OpenAI Tokens

We recommend storing your OpenAI token in a .env file. To create it, run the following command:

```Python
echo "OPENAI_API_KEY=your-api-key" > .env
```

To load the token in your notebooks, include the following code:  

```Python
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) 
openai.api_key = os.environ['OPENAI_API_KEY']
```

Now you're all set to make the most of OpenAI's capabilities within your notebooks. We hope you find this Jupyter Notebook tutorial using Python on macOS helpful. Happy exploring!

➡️

****Next steps:**** Now that you've set up Python and Jupyter notebook, take the next step by learning how to [create, store and query OpenAI embeddings using PostgreSQL and pgvector](https://timescale.ghost.io/blog/postgresql-as-a-vector-database-create-store-and-query-openai-embeddings-with-pgvector/).