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:
- Disabling any runner extensions ("code runner")
- Confirming the file is in
UTF-8
format here - Updating your library
pip3 install -U emoji
- Re-writing your code (not copy pasting)