Search code examples
pythonbatch-fileurlwgetanaconda3

Wget gives 'unknown url type: https' when run from batch file but not from terminal (python)


I get the error

urllib.error.URLError: <urlopen error unknown url type: https>

when trying to download a link using wget using a batch file but it works fine using python from the terminal. More detailed description of the problem:

When I run the python script:

import wget
url = 'https://ice-glaces.ec.gc.ca/www_archive/AOI_11/Coverages/rgc_a11_20230306_CEXPREA.zip'
wget.download(url, "C:/Users/zq19140/Downloads/")

from the terminal in my environment it works fine and downloads the correct file from the https link. However, I'm now trying to run this code using a batch file:

"C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\python.exe" "C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src\test_url.py"
pause

The 'icesat' environment I'm running python from is the same as the environment in which the python script runs from the terminal.

However, when I run this batch file I get the following error:

C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src>"C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\python.exe" "C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src\test_url.py"
Traceback (most recent call last):
  File "C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src\test_url.py", line 5, in <module>
    wget.download(url, "C:/Users/zq19140/Downloads/")
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\site-packages\wget.py", line 526, in download
    (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 548, in _open
    'unknown_open', req)
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\zq19140\AppData\Local\Continuum\anaconda3\envs\icesat\lib\urllib\request.py", line 1420, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: https>

C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src>pause
Press any key to continue . . .

python version: 3.7.10 // wget version: 3.2

--

I have tried:

  • adding import sys; print(sys.executable) and import site; print(site.getsitepackages()) this prints the same results when running from terminal and batch file, so I'm sure it's the same environment/python.

A very similar question is asked here but was never answered.


Solution

  • After help from Mofi in the comments this as the batch file was the solution:

    rem Define here the path to your conda installation
    set CONDAPATH=C:\Users\zq19140\AppData\Local\Continuum\anaconda3\
    rem Define here the name of the environment
    set ENVNAME=icesat
    
    if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)
    call %CONDAPATH%\Scripts\activate.bat %ENVPATH%
    
    python "C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src\auto_download_new_chart.py"
    python "C:\Users\zq19140\OneDrive - University of Bristol\Documents\Projects\JGI_seedcorn\src\auto_predict_sit_shp.py"
    
    call conda deactivate