question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"SyntaxError: 'await' outside function" when using Run cell

See original GitHub issue

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

IPython allows using await keyword outside functions, which is very helpful for debugging (%autoawait magic).

Consider the following snippet:

async def ping():
    return "pong"

await ping()

What steps reproduce the problem?

  1. Create new file, copy above snippet
  2. Run > Run cell

What is the expected output? What do you see instead?

  • Expected output: “pong”
  • Actual result: SyntaxError: ‘await’ outside function

Note that selected the code and performing Run > Run selection (F9) gives the expected “pong”.

Running the file (Run >Run, F5) also gives a SyntaxError, which I consider being the correct behaviour.

As a comparison, Visual Code gives a SyntaxError when running the file, but runs successfully when running the cell. For me, this is the best behaviour, hence the present ticket.

Versions

Freshly installed from conda create -n spyder spyder

  • Spyder version: 4.2.3 None

  • Python version: 3.9.2 64-bit

  • Qt version: 5.9.7

  • PyQt5 version: 5.9.2

  • Operating System: Windows 10

  • IPython 7.21.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ccordoba12commented, Mar 17, 2021

Hey @nbud, thanks for reporting. I didn’t know that IPython allows you to do that.

For now, I think you need to switch from using the runcell command to paste the contents of cells directly into the console. For that, you need to go to the menu:

Tools > Preferences > Editor > Run Code

and activate the option called Copy full cell contents to the console when running code cells.

0reactions
ccordoba12commented, Mar 17, 2021

Therefore it doesn’t really make sense to expect a random await in a python file.

Ok, I understand that and it makes sense to me.

I am not sure what the solution is here.

I think the only possible solution for now is to avoid using runcell, as I mentioned above. So I’m going to close this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I only use the await keyword inside of async ...
This is of course a SyntaxError: 'await' outside async function . But—if this code could run—while the await expression is not placed inside...
Read more >
'await' outside function error
I'm using the python environment and I'm having an error saying "SyntaxError: 'await' outside function". Does anyone know how to fix this?
Read more >
SyntaxError: 'await' outside function · Issue #2971
I run the example in PySyft/examples/tutorials/advanced/websockets-example-MNIST-parallel/Asynchronous-federated-learning-on-MNIST.ipynb.
Read more >
await - JavaScript - MDN Web Docs
Using await pauses the execution of its surrounding async function until the promise is settled (that is, fulfilled or rejected).
Read more >
[ASYNCPRAW] Cannot use async or await in asyncpraw
I am trying to run this code given in the quickstart: from config import * import asyncpraw import ... SyntaxError: 'await' outside function....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found