Short answer: Put below code in a celll at the top of your notebook, and make sure it is executed.
import os
os.environ[‘QUANDL_API_KEY’] = ‘your_api_key_here’
Long answer: I asked ChatGPT and got this:
To resolve the error message:
ValueError: Please set your QUANDL_API_KEY environment variable and retry.
you need to set your Quandl API key as an environment variable so that Zipline can access it. Here’s how you can do that:
1. Obtain Your Quandl API Key
-
Sign Up or Log In: Visit Quandl (now Nasdaq Data Link) and create an account if you haven’t already.
-
Find Your API Key: After logging in, go to your account settings to locate your unique API key.
2. Set the QUANDL_API_KEY
Environment Variable
You have several options to set the environment variable:
Option A: Set Within Your Jupyter Notebook
Add the following code at the beginning of your notebook:
import os
os.environ['QUANDL_API_KEY'] = 'your_api_key_here'
Replace 'your_api_key_here'
with the API key you obtained from Quandl.
Option B: Set in the Terminal Before Launching JupyterLab
If you launch JupyterLab from a terminal or command prompt, you can set the environment variable there.
-
For Unix/Linux/macOS:
export QUANDL_API_KEY=your_api_key_here
jupyter lab
-
For Windows Command Prompt:
set QUANDL_API_KEY=your_api_key_here
jupyter lab
Option C: Set as a System Environment Variable
This method makes the environment variable available system-wide.
-
Windows:
- Open Control Panel > System and Security > System.
- Click on Advanced system settings.
- Click Environment Variables.
- Under User variables or System variables, click New.
- Set the Variable name to
QUANDL_API_KEY
and Variable value to your API key.
- Click OK to save.
-
macOS/Linux:
-
Open your terminal.
-
Edit your shell profile file (e.g., ~/.bash_profile
, ~/.bashrc
, or ~/.zshrc
).
-
Add the following line:
export QUANDL_API_KEY=your_api_key_here
-
Save the file and run source ~/.bash_profile
(replace with your profile file) to apply the changes.
3. Restart JupyterLab
After setting the environment variable, restart your JupyterLab session to ensure the changes take effect.
Alternative: Pass the API Key Directly in Code
If you prefer not to set an environment variable, you can pass the API key directly in your code (if the library supports it):
from zipline.data import bundles
bundles.register('quantopian-quandl', bundles.quandl.QuandlBundle(api_key='your_api_key_here'))
Note: Replace 'your_api_key_here'
with your actual Quandl API key.
By setting the QUANDL_API_KEY
environment variable or passing the API key directly in your code, you should eliminate the error and be able to use Zipline with Quandl data successfully.