Home > OS >  Python code running in VS Code terminal but throwing error in output tab
Python code running in VS Code terminal but throwing error in output tab

Time:03-08

I'm trying to run a simple Python code in VS Code that runs successfully in the terminal but throws an error in the output tab.

import emoji

print(emoji.emojize('I :red_heart: Python!'))

This is the terminal result:

>>> import emoji
>>> print(emoji.emojize('I :red_heart: Python!'))
I ❤️ Python!

This is the error shown in the Output tab of VS Code once I run the code:

Traceback (most recent call last):
  File "c:\Users\subhr\OneDrive - University of St. Thomas\Python Practice\emoji_demo.py", line 3, in <module>
    print(emoji.emojize('I :red_heart: Python!'))
  File "C:\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-3: character maps to <undefined>

I'm using the Code Runner for Visual Studio Code extension.

CodePudding user response:

This seems to be a known issue with the Code Runner extension (see also this issue).

The recommended work-around there is to use the below setting (File->Preference->Settings) to run code in Integrated Terminal:

{
    "code-runner.runInTerminal": true
}

CodePudding user response:

I found a workaround that necessarily doesn't fix the issue. Once I disabled the "code runner" extension on vs code, I ran the code again but this time it showed a "ModuleNotFoundError: No module named 'emoji'". I then changed the interpreter on the command palette from my virtual environment Python to Global Python and then running the code worked like a charm. Although, I still haven't figured out why my code was throwing an error as it is.

CodePudding user response:

For future visitors, try:

  1. Disabling any runner extensions ("code runner")
  2. Confirming the file is in UTF-8 format here
  3. Updating your library pip3 install -U emoji
  4. Re-writing your code (not copy pasting)
  • Related