gpt4 book ai didi

Importing requests into Python using Visual Studio Code(使用Visual Studio代码将请求导入到Python中)

转载 作者:bug小助手 更新时间:2023-10-26 20:35:56 26 4
gpt4 key购买 nike



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).

我正在尝试将模块请求导入到一个Python文件中(使用Python2.7.14)。



Visual Studio Code outputted this in the console:

Visual Studio代码在控制台中输出了以下内容:




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.

Sudo pip安装请求,基于这一回答获得了无数的好评。



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.

我关闭了VS代码,并重新启动它,打开我的Python文件,运行它,我得到了同样的错误。我接着尝试了每一种解决方案,希望其中一种能奏效。没有一个人这样做。



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医生、BREW修剪等之后,我从头开始重新安装了Python。



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:

我还深入研究了Visual Studio代码中的代码设置,看看这是否就是我的问题所在。其中一个建议是覆盖code-runner.ecutorMap设置中的python设置,所以我在终端中输入了哪个python来获取到python的路径,并将vs Code的用户设置更新为python返回的路径。现在,我使用它作为我的代码运行器。



"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:

当我将导入请求放在文件的顶部时,我收到了这个错误,它下面的任何东西都不会执行:




[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.

我将我的文件命名为其他名称,所以我认为我的问题存在于“tempCodeRunnnerFile.py”中。我尝试删除codeRunner.ecutorMap的覆盖,但似乎也不起作用。



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

我没办法了。如果你有一个,我欢迎你的建议。感谢您的阅读。


更多回答

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 stackoverflow.com/questions/24664435/…

也许与你的问题无关,但你真的应该用一台游戏机。除此之外,还有sudo/usr/bin/python-m pip安装请求。PIP不一定指向您认为的解释器,另一种选择是专门使用PIP2。您可能还会发现这个有用的stackoverflow.com/Questions/24664435/…

@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.

@PadraicCunningham谢谢。我试过了,在控制台:/usr/bin/python:没有名为pip的模块。我在终端中输入了哪个pip,结果输出如下:/usr/local/bin/pip。

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

您的Defo在那里有多个安装,这将在https://bootstrap.pypa.io/get-pip.py&&sudo/usr/bin/pythonget-pin.py中工作,但除非您有不同的要求,否则我也会使用>=py3.6,并且一定会使用venv。一条蟒蛇也会提供信息,哪条-一条蟒蛇2,哪条-一条蟒蛇3

Thank you! Me RN youtube.com/watch?v=9N0OHdRFcJA Post as an answer and I'll accept.

谢谢大家!我RN youtube.com/watch? v= 9 N 0 OHDRFcJA帖子作为答案,我会接受。

@Adrian, no worries, I added an answer.

@禤浩焯,别担心,我加了一个答案。

优秀答案推荐

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-pin.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:

要进行调试,请在注释输出时执行以下操作:



/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”的名称应该可以很好地工作。



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)导入到代码文件



enter image description here




  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 .



Just remove the python version(s) that are currently on your Windows machine and download it from the Microsoft store.
Tried everything, this was the only one that actually worked.

只需删除您Windows计算机上当前的Python版本(S),然后从微软商店下载即可。我试过了所有方法,但这是唯一有效的方法。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com