Jupyter Notebook Tutorial: Setting Up Python & Jupyter Notebooks on macOS for OpenAI Exploration
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 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: to manage multiple Python installations on your machine, allowing different Python versions for various projects.
- Virtualenv: to create isolated environments for your Python projects, ensuring independent installations of packages and dependencies.
- Jupyter Notebook: 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:
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):
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:
pyenv install 3
pyenv global 3
Now, let's create a virtual environment for your project:
pyenv virtualenv 3 jupyter_env
pyenv activate jupyter_env
At any time, you can view your virtual environments by running:
pyenv virtualenvs
Install Jupyter Notebook in your new environment:
pip install jupyterlab
jupyter lab build
Create a directory for your project:
mkdir my_jupyter_project
cd my_jupyter_project
To install some dependencies, execute the following command:
pip install openai python-dotenv
Launch Jupyter Lab:
Jupyter-lab
You're now ready to explore:
- Our tutorial on OpenAI embeddings.
- The OpenAI cookbooks.
Extra, Extra! Storing OpenAI Tokens
We recommend storing your OpenAI token in a .env file. To create it, run the following command:
echo "OPENAI_API_KEY=your-api-key" > .env
To load the token in your notebooks, include the following code:
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!