Search code examples
pythonvisual-studio-codepython-requests

Importing requests into Python using Visual Studio Code


Preface: I've tried every suggestion in this post. None of them work.

I'm attempting to import the module requests into a Python file (using Python 2.7.14).

Visual Studio Code outputted this in the console:

ImportError: No module named requests

Upon digging, I discovered I don't have requests installed, so I fixed that with the following commannd from Terminal:

sudo pip install requests, based on this answer with a bazillion upvotes.

I closed VS Code and restarted it, opened my Python file, ran it and I got the same error. I proceeded to try each of the solutions in hopes one would work. None did.

I recently installed anaconda and I suspect that is the source of my problem, so I uninstalled every instance of Python I could find via brew and also stray installations that were parts of other installations that have accumulated over time on my hard disk based on this answer.

I then reinstalled python from scratch after running brew doctor, brew prune, etc.

I also dug into the code settings within Visual Studio Code to see if perhaps that's where my problem was. One of the suggestions was to override the settings for python in the code-runner.executorMap setting, so I typed which python in Terminal to obtain the path to python and updated VS Code's User Settings to the path which python returned. Now, I'm using this as my code-runner.executorMap for python:

"code-runner.executorMap": {
    "python" : "/usr/bin/python"
}

I've verified Python is working by throwing in a couple of simple statements in:

print("Printing works fine")
print(1+1)

The moment I put import requests at the top of the file, I get this error and nothing below it executes:

[Running] /usr/bin/python "/Users/me/Documents/developerNew/python/tempCodeRunnerFile.py" Traceback (most recent call last): File "/Users/me/Documents/developerNew/python/tempCodeRunnerFile.py", line 1, in import requests ImportError: No module named requests

I have my file named something else, so I think my problem lives in the "tempCodeRunnnerFile.py". I tried removing the override for the codeRunner.executorMap, but that doesn't seem to work either.

I'm out of ideas. If you have one, I welcome your suggestion. Thank you for reading.


Solution

  • The main issue is pip refers to some interpreter other than /usr/bin/python, the quick solution is to install pip using get-pip.py:

    wget https://bootstrap.pypa.io/get-pip.py && sudo /usr/bin/python get-pip.py 
    

    To debug, which pip as you commented outputs:

    /usr/local/bin/pip
    

    So pip is there, it just points to some other interpreter, on my linux box if I check each variation of pip:

    padraic@dell:~$ which pip
    /usr/local/bin/pip
    padraic@dell:~$ which pip2
    /usr/local/bin/pip2
    padraic@dell:~$ which pip3
    /usr/local/bin/pip3
    

    We see /usr/local/bin/pip refers to my python3 interpreter.

    By far a better option is to use a venv and preferably python3, python3.6 has a multitude of huge improvements over all previous releases, to create a venv:

    python -m venv venv 
    

    It's a while since I used vscode but from memory I think you can use workspaceRoot to set the path, I use venv consistently as my virtualenv name so something like "python.pythonPath": "${workspaceRoot}/venv/bin/python" should work fine.

    To install packages for the venv you just need to activate:

    . venv/bin/activate
    pip install ....
    

    Using venv's will save you a lot of headaches in the long run and greatly lessen the chance of screwing up your OS.