[英]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).我正在尝试将模块
requests
导入 Python 文件(使用 Python 2.7.14)。
Visual Studio Code outputted this in the console: Visual Studio Code 在控制台中输出了这个:
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:挖掘后,我发现我没有安装
requests
,所以我用终端的以下命令修复了它:
sudo pip install requests
, based on this answer with a bazillion upvotes. sudo pip install requests
,基于这个答案,获得了无数赞成票。
I closed VS Code and restarted it, opened my Python file, ran it and I got the same error.我关闭 VS Code 并重新启动它,打开我的 Python 文件,运行它,我得到了同样的错误。 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 .我最近安装了
anaconda
,我怀疑这是我问题的根源,所以我卸载了我可以通过brew
找到的每个 Python 实例,还卸载了杂散安装,这些安装是其他安装的一部分,这些安装是基于这个答案随着时间的推移在我的硬盘上积累的.
I then reinstalled python from scratch after running brew doctor
, brew prune
, etc.然后我在运行
brew doctor
、 brew prune
等之后从头开始重新安装 python。
I also dug into the code settings within Visual Studio Code to see if perhaps that's where my problem was.我还深入研究了 Visual Studio Code 中的代码设置,看看是否这就是我的问题所在。 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.其中一项建议是覆盖
code-runner.executorMap
设置中的python
设置,因此我在终端中键入which python
以获取python
的路径并将 VS Code 的用户设置更新为which python
返回的路径。 Now, I'm using this as my code-runner.executorMap
for python
:现在,我将其用作
python
的code-runner.executorMap
:
"code-runner.executorMap": {
"python" : "/usr/bin/python"
}
I've verified Python is working by throwing in a couple of simple statements in:我已经通过在以下几个简单的语句中加入几个简单的语句来验证 Python 是否正常工作:
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:在我将
import requests
放在文件顶部的那一刻,我收到此错误并且下面没有任何内容执行:
[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
[运行] /usr/bin/python "/Users/me/Documents/developerNew/python/tempCodeRunnerFile.py" 回溯(最近调用最后):文件 "/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".我将我的文件命名为其他名称,所以我认为我的问题出在“tempCodeRunnnerFile.py”中。 I tried removing the override for the
codeRunner.executorMap
, but that doesn't seem to work either.我尝试删除
codeRunner.executorMap
的覆盖,但这似乎也不起作用。
I'm out of ideas.我没主意了。 If you have one, I welcome your suggestion.
如果你有,我欢迎你的建议。 Thank you for reading.
感谢您阅读。
VSCode seems to let you import like this, VSCode 好像让你这样导入,
import pip._vendor.requests
or或
from pip._vendor import requests
Not sure why this happens.不知道为什么会发生这种情况。 But this happens!
但这发生了!
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
:主要问题是
pip
指的是除/usr/bin/python
之外的一些解释器,快速解决方案是使用get-pip.py
安装 pip :
wget https://bootstrap.pypa.io/get-pip.py && sudo /usr/bin/python get-pip.py
To debug, which pip
as you commented outputs:要调试,当您评论输出时,
which pip
:
/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:所以
pip
就在那里,如果我检查 pip 的每个变体,它只是指向我的 linux 机器上的其他一些解释器:
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.我们看到
/usr/local/bin/pip
指的是我的python3
解释器。
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
:到目前为止,更好的选择是使用
venv
,最好是python3
, python3.6
比所有以前的版本有许多巨大的改进,以创建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.自从我使用 vscode 已经有一段时间了,但从记忆中我认为你可以使用workspaceRoot来设置路径,我一直使用
venv
作为我的 virtualenv 名称,所以类似于"python.pythonPath": "${workspaceRoot}/venv/bin/python"
应该工作正常。
To install packages for the venv
you just need to activate:要为
venv
安装软件包,您只需要激活:
. 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.从长远来看,使用venv将为您省去很多麻烦,并大大减少搞砸操作系统的机会。
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 1)下载包https://files.pythonhosted.org/packages/f5/4f/280162d4bd4d8aad241a21aecff7a6e46891b905a4341e7ab549ebaf7915/requests-2.23.0.tar.gz
2)Extract it 2)提取它
3)Copy it in the visual studio project folder 3)复制到visual studio项目文件夹中
4)Include it in the project 4)将其包含在项目中
5)Import to the Code file 5)导入到代码文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.