Last updated: Apr 11, 2024
Reading time·4 min
pip install
from behind a proxyThe article addresses the following 2 related errors:
The Python "socket.gaierror: [Errno 11001] getaddrinfo failed" occurs for 2 main reasons:
http_proxy
environment variable.If you get the error when using the socket.getaddrinfo() method, make sure you haven't passed it incorrect arguments.
For example, if you're trying to connect to a server running on a localhost
port, your getaddrinfo()
call would look something like this:
import socket # [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 8000))] print(socket.getaddrinfo('localhost', 8000))
The first argument we passed to the method is the host and the second is the port.
If that doesn't work, you can also try to replace localhost
with the string
'127.0.0.1'
.
import socket # [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('127.0.0.1', 8000)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('127.0.0.1', 8000))] print(socket.getaddrinfo('127.0.0.1', 8000))
The string 127.0.0.1
is equivalent to localhost
but resolves more often.
You can also call the socket.getaddrinfo()
method with a fully qualified
domain name.
import socket print(socket.getaddrinfo('google.com', 443))
However, make sure the protocol scheme is not included because that causes an error.
import socket # ⛔️ socket.gaierror: [Errno -2] Name or service not known print(socket.getaddrinfo('https://google.com', 443))
Including the protocol scheme (https://
or http://
) causes an error.
If you need to handle the potential error, use a try/except
block.
import socket try: print(socket.getaddrinfo('google.com', 443)) except socket.gaierror as e: print('An error occurred: ', e)
We try to call the socket.getaddrinfo()
method in the try
block.
If a socket.gaierror
exception occurs, it gets handled in the except
block.
The error also commonly occurs when you try to connect to a network from behind a proxy.
You might have set the http_proxy
or https_proxy
environment variables by
mistake.
You can print the values of the variables by using the following commands on Windows.
# Windows CMD echo %http_proxy% echo %https_proxy%
If you are on macOS or Linux, issue the following commands instead.
# macOS and Linux echo $http_proxy echo $https_proxy
To clear the http_proxy
and https_proxy
environment variables on Windows,
issue the following commands in CMD (Command Prompt).
# for Windows CMD (Command Prompt) set http_proxy=null set https_proxy=null set HTTP_PROXY=null set HTTPS_PROXY=null
If you use PowerShell on Windows, issue the following commands instead.
# for Windows CMD (PowerShell) [Environment]::SetEnvironmentVariable('http_proxy', '', 'User') [Environment]::SetEnvironmentVariable('http_proxy', '', 'Machine') [Environment]::SetEnvironmentVariable('https_proxy', '', 'User') [Environment]::SetEnvironmentVariable('https_proxy', '', 'Machine')
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS, Linux or Windows Git Bash unset http_proxy unset https_proxy unset HTTP_PROXY unset HTTPS_PROXY
Check if the error is resolved after removing the http_proxy
and https_proxy
environment variables.
If you need to use a proxy, try setting the environment variables to the correct value.
For example, if you are on Windows, the commands would look something like this:
# for Windows # without a username and a password set HTTP_PROXY=http://proxy.company:8080 # or with a username and a password set HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.company:8080
If you are on macOS or Linux, the commands would look something like this:
# For macOS or Linux # without a username and a password export http_proxy=http://proxy.company:8080 export HTTP_PROXY=http://proxy.company:8080 # or with a username and a password export https_proxy=http://USERNAME:PASSWORD@proxy.company:8080 export HTTPS_PROXY=http://USERNAME:PASSWORD@proxy.company:8080
Make sure to replace the placeholders with the actual values.
pip install
from behind a proxyIf you're trying to use pip install
from behind a proxy, set the --proxy
parameter.
pip install --proxy http://user:password@proxy_server:port requests
If you don't have to specify the username and password, your command might look something like this:
pip install --proxy http://proxy_server:port
If you set the http_proxy
or https_proxy
environment variables, you don't
have to set the --proxy
parameter when running pip install
.
Make sure your connection to the internet is not flake, as that also causes the error.
If the server you're trying to connect to requires the use of a VPN, make sure your VPN is turned on.
You can also try to restart your VPN or try to disable it.
You can learn more about the related topics by checking out the following tutorials: