skip to Main Content

Shortly, a script of mine that had run just fine stopped working, throwing a

ModuleNotFoundError: No module named 'requests.auth'

I don’t remember having changed the Conda Environment the script uses.

Everything worked fine one day and stopped the next. I don’t know which parameter has changed.

I tried several paths to find a solution but was not successful. Among them were:

  • seting up a new, clean conda environment, definitely installed requests

  • clearing VS Codes cache

  • several reboots

The strange part is this: When I run the following commands in an interactive session in the shell, it works and I receive a "Response [200]".

When I run it in a Jupyter Notebook or .py file in VS Code, I get above mentioned ModuleNotFoundError. Same goes for executing the file from the command line.

All above tests were conducted in the same Conda environment which definitely has requests installed.

Also: It does work in a virtual environment I have access to to which my VS Code settings are synced. So maybe not VS Code but something local to my machine?

import requests
from requests.auth import HTTPBasicAuth

basic = HTTPBasicAuth('user', 'pass')

requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic)

Some specs:

  • OS: Windows 10

  • Python: 3.12.2

  • Requests: 2.31.0

  • Conda: 24.1.2

I’m grateful for any suggestions that nudge me in the right direction.

2

Answers


  1. Chosen as BEST ANSWER

    Eureka! I've found the answer at last.

    As expected, it was a rather local issue. The issue was not only limited to my machine, but also specific to my project. There was a sub-directory I had thoughtlessly named "requests" within my project directory. Upon renaming this mischievous little culprit, everything started working again.

    Thanks to @JRazor for pointing me in the right direction.


  2. Check your python interpreter by shortcuts "Ctrl+Shift+P" and type "Python: Select Interpreter" to choose your correct python interpreter.

    If it doesn’t work, try command pip install requests.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search