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

Viewed 2486

If I try to execute the following python code

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. So I found this link: 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.

3 Answers

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

So I added the following paths to the environment variable PATH for my user:

  1. Open environment variable for my user (to access the environment variable just type environment variable in your windows search bar.)

  2. Select the PATH variable and klick 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>\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.

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). It appears to be a permissions issue. Try to start the Windows Command Line as an administrator (right-click and Run as Administrator). 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.

  • Open Anaconda Prompt in Admin Mode
  • Run this command conda install -c anaconda requests
  • Install that package
  • Then run your code again. It should work now
Related