Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
Adrian Avatar asked Feb 13 '18 21:02

Adrian


People also ask

How do I import packages into VS Code?

Import packages#Run the command Go: Add Import to get a list of packages that can be imported to your Go file. Choose one and it will get added in the import block of your Go file.

Is Visual Studio code good for Python?

Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters.


2 Answers

VSCode seems to let you import like this,

import pip._vendor.requests

or

from pip._vendor import requests

Not sure why this happens. But this happens!

like image 173
Naveen S Avatar answered Sep 17 '22 11:09

Naveen S


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.

like image 35
Padraic Cunningham Avatar answered Sep 18 '22 11:09

Padraic Cunningham