简体   繁体   English

Python 模块请求在 anaconda 提示符下工作但不在 Windows 10 命令行中工作

[英]Python module requests working in anaconda prompt but not in windows 10 command line

If I try to execute the following python code如果我尝试执行以下 python 代码

import requests
data = requests.get('https://www.bbc.com')

I get the error message我收到错误消息

raise SSLError("Can't connect to HTTPS URL because the SSL "
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.

Same python code executed in the anaconda prompt just works fine.在 anaconda 提示符中执行的相同 python 代码工作正常。 So I found this link: A module is working in Anaconda prompt, but not in Spyder所以我找到了这个链接: A module is working in Anaconda prompt, but not in Spyder

Executing the command given in the stackoverflow post I get the path: C:\\Users\\x1\\Anaconda3\\python.exe Calling this path from a windows 10 command line and executing the code above still raises the error.执行 stackoverflow 帖子中给出的命令,我得到了路径: C:\\Users\\x1\\Anaconda3\\python.exe从 Windows 10 命令行调用此路径并执行上面的代码仍然会引发错误。

  • Open Anaconda Prompt in Admin Mode在管理员模式下打开 Anaconda Prompt
  • Run this command conda install -c anaconda requests运行这个命令 conda install -c anaconda requests
  • Install that package安装那个包
  • Then run your code again.然后再次运行您的代码。 It should work now现在应该可以使用了

I had a similar issue where running a python script as a scheduled task would raise this error (whereas running the script from Anaconda Prompt or from Spyder would work as expected).我有一个类似的问题,将 python 脚本作为计划任务运行会引发此错误(而从 Anaconda Prompt 或 Spyder 运行脚本会按预期工作)。 It appears to be a permissions issue.这似乎是一个权限问题。 Try to start the Windows Command Line as an administrator (right-click and Run as Administrator).尝试以管理员身份启动 Windows 命令行(右键单击并以管理员身份运行)。 It should work.它应该工作。

To achieve this as a scheduled task, instead of calling a batch file which then called python and then the script, call python directly from the task and add the script as the first argument.要将其作为计划任务实现,而不是调用批处理文件,然后调用 python 和脚本,直接从任务调用 python 并将脚本添加为第一个参数。

I had a similar issue (same error message related to the requests module) with a virtual environment of python 3.8.3 on a windows 10 virtual machine and no admin rights.我在windows 10虚拟机上的python 3.8.3虚拟环境中遇到了类似的问题(与请求模块相关的相同错误消息)并且没有管理员权限。 The solution for me actually was the PATH environment variable as described in the answer of Paul Stevens here: Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website .对我来说,解决方案实际上是PATH环境变量,如 Paul Stevens 在这里的回答中所述: Requests (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.") Error in PyCharm requesting website

So I added the following paths to the environment variable PATH for my user:所以我为我的用户在环境变量PATH添加了以下路径:

  1. Open environment variable for my user (to access the environment variable just type environment variable in your windows search bar.)为我的用户打开environment variable (要访问环境变量,只需在 Windows 搜索栏中键入environment variable 。)

  2. Select the PATH variable and klick edit选择PATH变量并点击edit

  3. Add the following paths (replace <path> with your actual path, for me it would be something like C:\\Program Files\\Anaconda3 etc.):添加以下路径(将<path>替换为您的实际路径,对我而言,它类似于C:\\Program Files\\Anaconda3等):

    • <path>\\Anaconda3
    • <path>\\Anaconda3\\scripts
    • <path>\\Anaconda3\\Library\\bin
  4. Open a new command window打开一个新的命令窗口

  5. Run your script运行你的脚本

And then it magically worked for me.然后它神奇地为我工作。

Btw the print(sys.executable) command would show me the same information in the command and in the anaconda prompt.顺便说一句, print(sys.executable)命令会在命令和 anaconda 提示符中显示相同的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM