1

This works in Jupyter notebooks but gives a SyntaxError: 'await' outside function in a local python script. I've imported all of the same python modules in the Jupiter notebook.

async def download(url, filename):
    response = await pyfetch(url)
    if response.status == 200:
        with open(filename, "wb") as f: # write binary mode
            f.write(await response.bytes())


path='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-ML0101EN-SkillsNetwork/labs/Module%204/data/Cust_Segmentation.csv' 

# download(path, "Cust_Segmentation.csv") # Produces this error: RuntimeWarning: coroutine 'download' was never awaited
await download(path, "Cust_Segmentation.csv")

and got this error

    await download(path, "Cust_Segmentation.csv")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function

Any idea what's going on? Remember, this works in Jupyter notebooks and NOT in

Solutions Attempted

I tried removing the await before calling the function and got this error: RuntimeWarning: coroutine 'download' was never awaited.

The following links do not appear to be relevant.

This may have some relevance, but I've tried removing the await and it still throws a different error: RuntimeWarning: coroutine 'download' was never awaited or UnboundLocalError: local variable '_jsfetch' referenced before assignment

Specific error with asyncio.run(download(URL, "Cust_Segmentation.csv")) is the following:

tail example.py

filename ="Cust_Segmentation.csv"

async def download(url, filename):
    response = await pyfetch(url)
    if response.status == 200:
        with open(filename, "wb") as f: # write binary mode
            f.write(await response.bytes())

asyncio.run(download(URL, "Cust_Segmentation.csv"))

Produces:

python3 example.py
/data/XXX/miniconda/envs/devel/lib/python3.10/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.1
  warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
Traceback (most recent call last):
  File "/mnt/d/DEMO/MachineLearning/k-means_clustering/example.py", line 33, in <module>
    asyncio.run(download(URL, "Cust_Segmentation.csv"))
  File "/data/XXX/miniconda/envs/devel/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/data/XXX/miniconda/envs/devel/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/mnt/d/DEMO/MachineLearning/k-means_clustering/example.py", line 28, in download
    response = await pyfetch(url)
  File "/data/XXX/miniconda/envs/devel/lib/python3.10/site-packages/pyodide/http.py", line 234, in pyfetch
    url, await _jsfetch(url, to_js(kwargs, dict_converter=Object.fromEntries))
UnboundLocalError: local variable '_jsfetch' referenced before assignment
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Michael Emerson
  • 11
  • 1
  • 3
  • 6

0 Answers0