Python's requests "Missing dependencies for SOCKS support" when using SOCKS5 from Terminal

115,818

Solution 1

I had the same issue with conda and requests 2.11 (I work in a Ubuntu VM behind a corporate proxy).

This issue helped me. I changed my environment variable all_proxy (which was originally set to a SOCK proxy socks://....) to the https version in my .bashrc file :

export all_proxy="https://<proxy>:<port>/"

and now it works.

Solution 2

This means that requests is using socks as a proxy and that socks is not installed.

Just run pip install pysocks

Solution 3

In Ubuntu you can run :
unset all_proxy && unset ALL_PROXY

Solution 4

I added the requests[socks]>=2.10.0 to my requirements.txt, updated my https_proxy env variable, and came across the above error. I then tried a regular pip install requests[socks] after resetting the https_proxy env variable and PySocks was installed. I'm not sure why the pip install -Ur requirements.txt failed to install PySocks the first time.

After that, I was able to make a request in python using the socks proxy.

It looks like your socks server is not behaving. I would see if you, or your admin, could watch the logs and see what the machine is complaining about.

Solution 5

I also stumbled upon this issue while doing a simple pip install -U pip, but information I found from your question helped me resolved my issue. I am on Mac OS X.

As you have pointed out, adapters.py from the requests package was trying to do this:

try:
    from .packages.urllib3.contrib.socks import SOCKSProxyManager
except ImportError:
    def SOCKSProxyManager(*args, **kwargs):
        raise InvalidSchema("Missing dependencies for SOCKS support.")

So it seems sensible to look for the place of definition of SOCKSProxyManager. It seems to be in a "contrib" module in urllib3 and not installed alongside urllib3 by default. The docstring of that module says:

This module contains provisional support for SOCKS proxies from within
urllib3. This module supports SOCKS4 (specifically the SOCKS4A variant) and
SOCKS5. To enable its functionality, either install PySocks or install this
module with the ``socks`` extra.

The instructions of the pip docs says this about setuptools extras:

6. Install a package with setuptools extras.

$ pip install SomePackage[PDF]
$ pip install git+https://git.repo/some_pkg.git#egg=SomePackage[PDF]
$ pip install SomePackage[PDF]==3.0
$ pip install -e .[PDF]==3.0  # editable project in current directory

So I followed the instructions and did:

$ pip install 'urllib3[socks]'

I then continued with pip install -U pip, which was what I was supposed to be doing, and now it works.

I wonder how many people got tricked-up by the square brackets, as Bash and other shells often treat it as a special character, which needs to be escaped for it to reach the invoked program (in this case, pip).

Share:
115,818
BringBackCommodore64
Author by

BringBackCommodore64

Do NOT forget they were once considered superior than the Apple computers! 1.023 MHz MOS Technology 8-bit microprocessor 64 kB of RAM (expandable to 320 kB) 20 kB ROM Operating system: BASIC 2.0 16 colors VIC-II graphic video chip 160 × 200 pixels screen 3-channel sound synthesizer 16 bit parallel I/O 2× CIA 6526 joysticks Floppy disk (better performance than the Atari 810 drive) ..and all that for just $595! Support the initiative to bring back to life the highest-selling single computer model of all time! .#BringBackTheCommodore64 Share and don't forget to retweet and retwat!

Updated on July 05, 2022

Comments

  • BringBackCommodore64
    BringBackCommodore64 almost 2 years

    I'm trying to interact with an API from my Python 2.7 shell using a package that relies on Python's requests. Thing is the remote address is blocked by my network (university library).

    So to speak to the API I do the following:

    ~$ ssh -D 8080 [email protected]
    

    And then, in new terminal, in local computer:

    ~$ export http_proxy=socks5://127.0.0.1:8080 https_proxy=socks5://127.0.0.1:8080
    

    Then I run the program in Python console but fails:

    ~$ python
    >>> import myscript
    >>> id = '1213'
    >>> token = 'jd87jd9'
    >>> connect(id,token)
    
    File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 518, in post
        return self.request('POST', url, data=data, json=json, **kwargs)
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
        resp = self.send(prep, **send_kwargs)
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
        r = adapter.send(request, **kwargs)
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
        conn = self.get_connection(request.url, proxies)
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 273, in get_connection
        proxy_manager = self.proxy_manager_for(proxy)
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 169, in proxy_manager_for
        **proxy_kwargs
      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 43, in SOCKSProxyManager
        raise InvalidSchema("Missing dependencies for SOCKS support.")
    requests.exceptions.InvalidSchema: Missing dependencies for SOCKS support.
    

    This excerpt is from the adapters.py requests module:

    > try:
    >     from .packages.urllib3.contrib.socks import SOCKSProxyManager except ImportError:
    >     def SOCKSProxyManager(*args, **kwargs):
    >         raise InvalidSchema("Missing dependencies for SOCKS support.")
    

    Now problem seems to be originated in urllib3's SOCKSProxyManager.

    So I read you can use SOCKSProxyManager with SOCKS5 if you have install PySocks or you do a pip install urllib3[socks]

    Alas, I tried both PySocks and urllib3 with Socks without any success.

    Any idea of another workaround?

    EDIT:

    I also tried pip install requests[socks] (that's requests 2.10.0 with Socks support) and I am getting this:

      File "/home/username/.virtualenvs/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 467, in send
        raise ConnectionError(e, request=request)
    requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='api-server.com', port=443): Max retries exceeded with url: /auth (Caused by NewConnectionError('<requests.packages.urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x95c7ccc>: Failed to establish a new connection: SOCKS5 proxy server sent invalid data',))
    
  • BringBackCommodore64
    BringBackCommodore64 over 7 years
    I'll test it your suggestion but as soon as I have the chance. For the moment I decided to work from elsewhere (not from the University). And I'll let you know. For the moment I'll upvote your answer. Thanks, Kirk!
  • BringBackCommodore64
    BringBackCommodore64 over 7 years
    Thanks! I totally ditched working from the library so I can't test. But I trust you!
  • BringBackCommodore64
    BringBackCommodore64 over 7 years
    Unfortunately I resolved the issue by not going to the library. So I can't give you feedback. But thanks anyway! :)
  • BringBackCommodore64
    BringBackCommodore64 about 7 years
    Good to know my issue helped you resolve yours. And thankfully yours will solve someone else's issue too!
  • Thane Brimhall
    Thane Brimhall over 6 years
    Yes, this was a non-obvious issue. For some reason, this module was not correctly installed via requirements.txt
  • JinSnow
    JinSnow over 6 years
    pip install request[socks] did NOT work, but pip install -U requests[socks] worked
  • JasonWayne
    JasonWayne over 6 years
    This works for me, however, we need to close the proxy first to install this module.
  • Jesbus
    Jesbus almost 6 years
    Thank you, your answer led me to fix my issue :)
  • Jiang YD
    Jiang YD almost 6 years
    but I AM using socks proxy
  • m3nda
    m3nda over 5 years
    -U --upgrade option alone will not work if you had some previous package installed. pip install -U requests[socks] --ignore-installed will work.
  • blubberdiblub
    blubberdiblub over 5 years
    @erm3nda --force-reinstall is somewhat cleaner than --ignore-installed, as it will clean up a (potentially different) previously installed package first. I use --ignore-installed only when I want to locally override a package that is installed system-wide (which can happen inside a virtual env set up with --system-site-packages).
  • Pavel Komarov
    Pavel Komarov over 5 years
    Hint for noobs: echo $all_proxy to see what your machine is currently set to. It's probably socks://<proxy>:<port>/, so all you have to do is replace the prefix.
  • benzkji
    benzkji almost 5 years
    either this, or pip install pysocks
  • Erik Aronesty
    Erik Aronesty over 4 years
    this fix it for me. thanks. even with all the deps installed, this is the error you will get if the socks:// prefix is used
  • iopq
    iopq over 4 years
    That's the problem, if pip install doesn't go through the proxy how am I supposed to install pysocks
  • a3k
    a3k over 4 years
    Wow, for the second time I face that problem, googling, and reaching your answer which is the solution I always forget :) If only stackoverflow allows upvoting multi times, I'd upvote this thousand times :)
  • CS QGB
    CS QGB over 2 years
    TypeError("__init__() got an unexpected keyword argument 'cert_file'")
  • nisc
    nisc over 2 years
    Well, this disables the proxy, which is not what OP asked for.
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • hgg end
    hgg end over 2 years
    This method works well, when I build docker image under setting vpn proxy.
  • aafirvida
    aafirvida about 2 years
    In 2022 This is still the correct answer to the question.
  • sify
    sify about 2 years
    you can install pysocks using tarball first