How to Use a Proxy in Jupyter Notebook: Environment and Code

Published:

11 minute read

Acar Diveroli
Written by: Acar Diveroli
Four stops on a rail: terminal, Jupyter server, kernel and a blue notebook cell, with ENV labels between them

A proxy setup in Jupyter is the set of environment variables and code settings that route the kernel process's outbound requests through an intermediary server. The kernel inherits these variables from the shell you started Jupyter in. A change made inside the notebook only affects later requests in that kernel. Some libraries never see the change at all. This post covers how the environment reaches the kernel, a one-cell requests session, how pandas and pip pick up the proxy, and keeping the password out of the notebook.

Where the kernel gets its environment

The kernel is a separate Python process that the Jupyter server starts. The server carries the environment variables of the shell you ran it from. When the kernel starts, it copies those variables. If the kernel type's kernel.json file has an env field, those values are added too.

So the most reliable path is to define the variable before Jupyter:

bash
export HTTPS_PROXY="http://user:pass@pr.proxynet.io:8000"
export HTTP_PROXY="$HTTPS_PROXY"
jupyter lab

In Windows PowerShell, the same step looks like this:

powershell
$env:HTTPS_PROXY = "http://user:pass@pr.proxynet.io:8000"
$env:HTTP_PROXY = $env:HTTPS_PROXY
jupyter lab

The address starts with http:// even when the target is HTTPS. The client asks the proxy for a tunnel and sets up TLS inside it. Where you set the value decides who sees it and how long it lives:

Where it's setWho sees itAfter a kernel restart
Shell that starts JupyterServer and every kernelStays
env in kernel.jsonThat kernel typeStays
os.environ or %env in a cellOnly that kernel and its child processesLost

If the proxy disappears after a kernel restart, the problem is in the last row. For a lasting setting, use the shell or kernel.json.

Setting it from the notebook

Changing os.environ in a cell only affects that kernel, and it doesn't work retroactively. Requests made after the variable is written can see it. Objects built earlier may keep their old state.

python
import os
from getpass import getpass

os.environ["HTTPS_PROXY"] = getpass("Proxy URL: ")
os.environ["HTTP_PROXY"] = os.environ["HTTPS_PROXY"]

Requests reads the variables again on every request. In our test we wrote the variable in a cell later on, and the next requests.get call went through the proxy. Python's urllib module behaves differently. On the first urlopen call it builds a default opener and reads the proxy setting at that moment. That object lives until the kernel shuts down. You'll see the consequence in the pandas section.

IPython's %env magic does the same job: %env HTTPS_PROXY=http://.... But the command prints the value. According to the IPython source, the output looks like env: HTTPS_PROXY=..., so the password lands in the output cell. For an address without a password, %env is fine. For one with a password, use the getpass cell.

A requests session in one cell

If every request in your code should go through the same proxy, collect the setting in a requests.Session object. The proxy is then defined in a single cell at the top of the notebook, and later cells only use the session object.

python
import os
from getpass import getpass
import requests

proxy_url = os.environ.get("PROXY_URL") or getpass("Proxy URL: ")

session = requests.Session()
session.trust_env = False  # keep HTTP(S)_PROXY from the environment from overriding this session
session.proxies = {"http": proxy_url, "https": proxy_url}

r = session.get("https://api.ipify.org", timeout=15)
print(r.status_code, r.text)

The trust_env = False line isn't decoration. According to the Requests docs, proxy variables in the environment override session.proxies. In our test with Requests 2.34.2, we set one proxy in the environment and a different one on the session. The request went through the environment's proxy. With trust_env = False, the session's address was used. This line also turns off other environment settings such as REQUESTS_CA_BUNDLE and .netrc. Per-request proxies= and other Requests details are in using proxies with Python Requests.

For SOCKS5, install the requests[socks] package and write the scheme as socks5h://. That way the target name is resolved on the proxy side. The Proxynet SOCKS5 Proxy also carries UDP, but the libraries in this post use TCP.

Keeping credentials out of the notebook

An .ipynb file is a JSON document that stores cell outputs along with the cell source. A password typed into a cell goes to everyone you share the file with. A password printed to the output takes the same path. Three places need care:

  • Cell source. A line like proxies = {"https": "http://user:pass@..."} is saved to the file as is.
  • %env output. An assigning %env writes the address to the output. A bare %env lists the whole environment. IPython only hides variables whose names contain key, token or secret. HTTPS_PROXY stays visible.
  • Debug print. print(session.proxies) or a dump of os.environ shows the password in plain text.

Error messages didn't show the password in our test. On a closed proxy port, Requests 2.34.2 printed only the host and port. Still, clear the outputs before sharing with Edit → Clear Outputs of All Cells in JupyterLab. For notebooks in a Git repository, a tool such as nbstripout strips outputs before each commit.

There are two clean ways to get the password in. getpass opens a password prompt and doesn't write the value into the cell. An environment variable keeps the value outside the notebook, in the shell or in kernel.json, before Jupyter starts. jupyter kernelspec list shows the kernel list. If the password contains @, # or :, percent-encode it in the address: pass@123 → pass%40123.

Reading URLs with pandas

pandas.read_csv and read_html open an address that starts with http:// or https:// through Python's urllib module. In the pandas source, the urlopen function in pandas/io/common.py calls urllib.request.urlopen directly. So the proxy comes from the environment variable. For HTTP addresses, the storage_options dict is sent as request headers, not as a proxy.

The trap is that urllib freezes the proxy setting on the first call. In our test we did the following, in order:

CellActionResult
1A URL was read before any proxy was setWent direct
2HTTP_PROXY written, a new URL readStill went direct, name not resolved
3Opener rebuiltWent through the proxy

pandas uses the same urlopen call, so this order affects it too. If a read_csv ran without a proxy at the top of the notebook, later reads also skip the proxy. Rebuilding the opener takes one line:

python
import urllib.request

urllib.request.install_opener(urllib.request.build_opener())

The more predictable route is to download the data with the requests session and hand pandas the text:

python
import io
import pandas as pd

r = session.get("https://data.example.com/prices.csv", timeout=30)
r.raise_for_status()
df = pd.read_csv(io.StringIO(r.text))
tables = pd.read_html(io.StringIO(r.text))  # for an HTML page

This way pandas never touches the network, and the proxy is managed in one place. One more note for Windows: if no proxy variable is set, urllib reads the system proxy setting from the registry. The cleanup steps after the download are in cleaning data with pandas.

pip and %pip

%pip runs python -m pip with the kernel's own Python in a separate process. The child process inherits the kernel's current environment. In our test we wrote the environment variable in the kernel process and then started pip in a child process. pip's request went through the proxy.

If the environment has no proxy, give pip the --proxy option:

python
%pip install --proxy http://user:pass@pr.proxynet.io:8000 pyarrow

The format in the pip docs is scheme://[user:passwd@]proxy.server:port. In our test we gave pip 26.2.2 pass%40123, and the proxy received pass@123. This line is also saved in the cell source. For an address with a password, run the getpass environment cell first, then write %pip install without the option. Use %pip rather than !pip: !pip may call the pip of a different Python on the shell's path.

The server proxy is a separate thing

Not every setting with "proxy" in its name in the JupyterLab UI or the extension list carries kernel requests. The jupyter-server-proxy extension lets you open a web app running next to the notebook through the Jupyter address. In JupyterHub, configurable-http-proxy routes users to their own servers. Both handle incoming traffic.

The kernel's outbound requests look at environment variables or the setting in your code. Server-side work, such as the extension manager, uses the server's environment. If you change a server setting and expect the kernel's IP to change, you're on the wrong layer.

Troubleshooting

The first check is whether the request really leaves through the proxy. Call https://api.ipify.org once with the session and once without it. If both answers match, the proxy isn't being applied.

SymptomLikely causeFix
Your own IP shows after a kernel restartThe setting was in a cellDefine it in the shell or kernel.json
requests uses the proxy, pandas goes directurllib opener was built with the old settinginstall_opener(build_opener())
session.proxies is ignoredAn environment variable overrides the sessiontrust_env = False
Target name not resolved errorThe request never reached the proxyCheck os.environ.get("HTTPS_PROXY")
407 responseSpecial character in the passwordPercent-encoding

For other cases where the environment variable doesn't reach the kernel, see why HTTP_PROXY isn't working.

How we tested

We ran the tests on December 2, 2026, on Windows 11, with Python 3.13.17, Requests 2.34.2, urllib3 2.6.3 and pip 26.2.2. Jupyter, IPython and pandas weren't installed on the machine. So we tested the cell logic with plain Python: each scenario ran in a new process, like a new kernel. On 127.0.0.1 we ran an HTTP proxy that logged each incoming request and the user in the Proxy-Authorization header. Targets were plain HTTP addresses, so the CONNECT tunnel was outside the test. We checked %pip behavior with a python -m pip index versions call in a child process. For %env, %pip and the pandas URL path, we read the IPython and pandas source code. We didn't run the pandas lines.

The bottom line

Define the proxy in the shell before Jupyter, or build a single requests session at the top of the notebook. Give pandas downloaded text instead of a URL, and use the environment or the --proxy option for pip. Above all, keep the password out of cells and outputs. If you'll pull data from notebooks regularly, the Residential Proxy page shows the coverage.

Sources

Frequently asked questions

How do I set a proxy in Jupyter Notebook?

There are two ways. The lasting way is to define HTTPS_PROXY and HTTP_PROXY in the shell before starting Jupyter. Every kernel inherits that value. The in-code way is to build a requests.Session in the first cell, fill its proxies field and set trust_env = False. Later cells use the same session, and the proxy is managed in one place.

Is it safe to set a proxy password with %env?

No. %env HTTPS_PROXY=... prints the value to the output cell as an env: line. The command itself also stays in the cell source. Both are saved to the .ipynb file, and anyone who gets the file sees the password. For an address with a password, read the value with getpass and write it to os.environ. Clear the outputs before sharing.

Does pandas read_csv use a proxy?

Yes, if an environment variable is set. pandas opens http and https addresses with Python's urllib module and takes the proxy from the environment. But urllib freezes the setting on the first call. If you wrote the variable later, refresh the opener with install_opener(build_opener()). The safer route is to download the file with the requests session and pass it to pandas through io.StringIO.

How does %pip install work behind a proxy?

%pip runs pip with the kernel's Python in a child process and inherits the kernel's environment. If HTTPS_PROXY is set there, nothing else is needed. If not, write %pip install --proxy http://user:pass@pr.proxynet.io:8000 package. Percent-encode special characters in the password. This line stays in the cell source. For an address with a password, it's safer to run the environment cell first and then a plain %pip install.

Why is the proxy setting lost when I restart the kernel?

A value written to os.environ in a cell lives only in that kernel process. On restart, the server opens a new process and copies the environment from its own variables. The change made in the cell isn't in that copy. To make it last, define the variable in the shell that starts Jupyter or in the env field of the kernel type's kernel.json.

Does JupyterLab's proxy setting affect kernel requests?

No. Extensions like jupyter-server-proxy open web apps running next to the server through the Jupyter address. JupyterHub's proxy routes each user to their own server. The kernel's outbound requests don't pass through either. The proxy the kernel uses comes from environment variables or the proxies setting in your code. Changing a server setting doesn't change the kernel's outbound IP.

Ask ChatGPTAsk Claude