32

Is there a similar config to that of .condarc (anaconda 4.0.0) that allows Jupyter to be configured to work behind a corporate proxy on a local machine?

Error received:

HTTPError: HTTP Error 407: Proxy Authentication Required
1
  • I fixed this by setting the HTTPS_PROXY and HTTPS_PROXY environment variables, correctly. My HTTPS one was incorrect, and failing similarly. Apr 4, 2017 at 2:25

8 Answers 8

26

Way easier: Just add the following to your notebook:

In [1]: import os
        os.environ['http_proxy'] = "http://user:passwd@host:port" 
        os.environ['https_proxy'] = "https://user:passwd@host:port" 

after that, requests will work OK=200, e.g.

In [2]: import requests
        requests.get("https://google.com")
Out[2]: <Response [200]>
3
  • Easier, but kind of cumbersome: You would have to repeat in every single notebook you create + You don't want these lines to appear in your notebook if you publish it
    – Oyono
    Feb 15, 2019 at 7:24
  • Feel free to concatenate strings from environment variables or config files that hold your password.
    – Boern
    Feb 18, 2019 at 8:46
  • 1
    was setting my proxy settings in the .condarc file, which allowed me to work with conda behind my company proxy. However, any request done via a JN (installed in a conda env) was not working. Adding this to my script solved the issue. Not sure why JN is not taking the proxy settings of the .condarc file but in any case this is solving my issue. Thanks
    – GCGM
    Dec 8, 2021 at 13:51
12

Based on this link.

You have to modify the Jupyter notebook server env. Create a file named 00-something.py under your Jupyter notebook server profile and add the following:

For example:

vi /.jupyter/profile_myserver/startup/00-startup.py

(or on Windows open up "C:/Users/your username/.jupyter/profile_myserver/startup/00-startup.py" in your editor of choice)

and add

import sys,os,os.path
os.environ['HTTP_PROXY']="http://proxy.example.com:80"
os.environ['HTTPS_PROXY']="https://proxy.example.com:443"

you can confirm the env variables by running

%env

in a cell and the output

{'CLICOLOR': '1',
'GIT_PAGER': 'cat',
'HOME': '/home/jay',
'HTTP_PROXY': 'http://proxy.example.com:80',
..

Next try

import requests
requests.get("http://google.com")

If you get a response [200] then you are all set.

6
  • What would be the procedure on Windows 7? May 2, 2016 at 12:06
  • Not sure but I would expect the only difference is where you find the server profile and possibly what you edit it with.
    – Jamie Bull
    May 2, 2016 at 12:10
  • the 's' letter is missing in the address for the https?
    – tagoma
    Oct 25, 2016 at 7:18
  • 3
    I added 00-startup.py with the above contents in C:\Users\my_username\.ipython\profile_default\startup folder. Issue fixed Apr 27, 2017 at 4:08
  • 2
    What should I do if I use a socks5 proxy? Thanks!
    – roachsinai
    May 5, 2018 at 2:16
2

Use the lowercase variable instead, it works for me:

import sys,os,os.path
os.environ['http_proxy']="http://user:passwd@host:port"
os.environ['https_proxy']="http://user:passwd@host:port"

Then check your env variable using this:

%env

The output will be like this:

{'CLICOLOR': '1',
 '...'
 '...'
 'http_proxy': 'http://gunawan.marbun:[email protected]:8080'
 'https_proxy': 'https://gunawan.marbun:[email protected]:8080'
 'no_proxy': 'localhost,127.0.0.0/8,::1'}

Notes: Since I can't comment due to my reputation (req 50 and I'm newbie), I present new answer instead.

1

An easier solution for me was to add an exception to my proxy configuration. I just put the address http://localhost:8888 to my exception list and it worked.

1

Based on these Jupyter customization instructions:

  1. Create the directory .jupyter_config in your home directory
  2. Add the line JUPYTER_CONFIG_DIR=~/.jupyter_config to your bash/shell profile (eg. .bash_profile).
  3. Add a script titled startup.py to ~/.jupyter_config with the following code, customized with your specific proxy information:
import os
os.environ['http_proxy']= "http://user:passwd@host:port"
os.environ['https_proxy']= "https://user:passwd@host:port"
os.environ['HTTP_PROXY']= os.environ['http_proxy']
os.environ['HTTPS_PROXY']= os.environ['https_proxy']
1

1. Ensure you are connected to VPN to office network

2. You can set up proxy before starting notebook or in every notebook

A. before starting

from jupyer notebok, start terminal (From option New on right top)

set HTTP_PROXY=http://fakeserver:fakeport
set HTTPS_PROXY=http://fakeserver:fakeport

jupyter notebook 

(This will start notebook in new terminal, with proxy set that can be checked with %env)

B. in every notebook

import sys,os,os.path
os.environ['HTTP_PROXY']="http://fakeserver:fakeport"
os.environ['HTTPS_PROXY']="http://fakeserver:fakeport"

3. in windows, set the environmental variables

This PC (Rt Click) >> propertis>> Advanced System Setting >> Advanced >> Envioronmental variables>> click NEW to add 2 variables pointing HTTP_PROXY and HTTPS_PROXY to "http://fakeserver:fakeport"
1

For those of you who may need a socks5 proxy, the following did the trick for me:

os.environ['http_proxy'] = "socks5h://host:port"
os.environ['https_proxy'] = "socks5h://host:port"

The only difference with the http proxy was the socks5h part in the url. In my case the proxy didn't have username and password but I suppose these too may be added to the url like socks5h://user:passwd@host:port.

0

Mostly the problem comes due to adding HTTPS in the proxy server URL. For both HTTP and HTTPS, the proxy server URL should be "http://" only

import os
os.environ['http_proxy'] = "http://username:complexpassword!@34@serverIP:port" 
os.environ['https_proxy'] = "http://username:complexpassword!@34@serverIP:port"



import requests
requests.get("https://google.com")

Response should be 200 OK

If you are in a Windows domain and use domain\username for authentication, just use username instead of domain\username in python setting above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.