9

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.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    Maybe unrelated to your problem but you should really use use a venv. Apart from that try, `sudo /usr/bin/python -m pip install requests`. pip does not necessarily point to the interpreter you think it does, another option is to specifically use `pip2`. You might also find this useful https://stackoverflow.com/questions/24664435/use-default-python-rather-than-anaconda-installation-when-called-from-the-termin/24664480#24664480 – Padraic Cunningham Feb 13 '18 at 21:25
  • @PadraicCunningham Thank you. I tried that and I get this in console: `/usr/bin/python: No module named pip`. I typed `which pip` in Terminal and it outputs this: `/usr/local/bin/pip`. – Adrian Feb 13 '18 at 22:10
  • 1
    You defo have multiple installs there, this will work `wget https://bootstrap.pypa.io/get-pip.py && sudo /usr/bin/python get-pip.py` but unless you have a distinct requirement I would also use `> =py3.6` and definitely use a venv. `which -a python` will also be informative as will `which -a python2` and `which -a python3` – Padraic Cunningham Feb 13 '18 at 22:15
  • Thank you! Me RN https://www.youtube.com/watch?v=9N0OHdRFcJA Post as an answer and I'll accept. – Adrian Feb 13 '18 at 22:22
  • What os are you running its confusing it seems like ur running or mac or linux but then u mention visual studio code – mgracer Feb 13 '18 at 22:32
  • @mgracer I'm running a native Mac distribution of VSC. https://code.visualstudio.com/download I'm squared away now. I had an issue that stemmed from multiple versions of Python. Patrick pointed me in the right direction to resolve the issue. – Adrian Feb 13 '18 at 22:38
  • Oh my bad i was thinking visual studio not code oops – mgracer Feb 13 '18 at 22:39
  • 1
    @Adrian, no worries, I added an answer. – Padraic Cunningham Feb 13 '18 at 22:47

4 Answers4

9

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!

Naveen S
  • 101
  • 1
  • 3
2

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

you have one more option you can follow the following steps:

1)Download the package https://files.pythonhosted.org/packages/f5/4f/280162d4bd4d8aad241a21aecff7a6e46891b905a4341e7ab549ebaf7915/requests-2.23.0.tar.gz

2)Extract it

3)Copy it in the visual studio project folder

4)Include it in the project

5)Import to the Code file

enter image description here

Kashif
  • 40
  • 2
0
  1. Go to PowerShell and run command - python
  2. See what interpreter you are getting - In my case it is Python 3.11.1
  3. Select Python 3.11.1 from view> command palette .