Installing python packages using pip can sometimes be a challenge in China. For some reason, from an aliyun instance, PyPI is sometimes simply too slow or unreachable. Here are a few solutions to get over this issue and install your sweet python packages.
Install through proxy
This is a bit of a quick and dirty solution but works really well for me. For this to work, you will need a “ladder” server accessible from China with a good connection to https://pypi.python.org/ and an SSH server running. The idea is to create a simple SOCKS5 proxy using OpenSSH and run pip commands through that proxy. Unfortunately, pip supports HTTP proxies only. No problem, we will be using proxychains to funnel all connections through our proxy.
Let’s install proxychains on our python machine and configure it to use our local SOCKS5 proxy.
$ sudo apt-get install proxychains
I choose to use a local configuration file in ~/.proxychains/proxychains.conf, see the man page for mode configuration options.
[ProxyList] socks5 127.0.0.1 1080
I can now start the SOCKS5 proxy through an SSH connection and run pip inside the tunnel.
$ ssh -4 -D 1080 email@example.com $ proxychains pip install -r requirements.txt
Install through local packages
You can also download packages directly from PyPI and install them locally. Depending on your pip version you’ll be able to do either of those two commands:
$ pip install --upgrade --no-index --find-links=/path/to/package.tar.gz
This will force you to download all the dependencies manually
$ pip install package.tar.gz
This option appeared in recent versions of pip, depencendies will be downloaded for you.