0

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.

14
  • Open the file, it should say UTF-8 in the bottom right of the vscode window Commented Mar 8, 2022 at 5:54
  • To be able to find the cause: could you test this: print(b'I \xe2\x9d\xa4\xef\xb8\x8f Python!'.decode('utf-8'))? It should give the same result. If this works, the problem was in your file encoding. If this doesn't work, the problem is in your output terminal and you should search how to change the character encoding for that terminal. Commented Mar 8, 2022 at 5:55
  • 1
    I'm using code runner extension for my vs code. I think the plugin has a bug. Commented Mar 8, 2022 at 5:57
  • 1
    Your file looks to be encoded in cp1252. As it's mentioning File "C:\Python\Python310\lib\encodings\cp1252.py" in your error Commented Mar 8, 2022 at 6:03
  • 1
    @FreddyMcloughlan It is UTF-8 file. I can see it in the bottom. Commented Mar 8, 2022 at 6:07

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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. Install the gremlins extention to search for bad characters
  5. Re-writing your code (not copy pasting)

2 Comments

Strangely, I just ran into the exact inverse issue. The output tab gave me correct results whilst running my code with the terminal enabled gave me an issue "Folder not found". After disabling the Code Runner extension, the problem got fixed! Could this be a conflict between extensions? Why code-runner doesn't work as intended with some languages? (C++ in my case)
@DanielMelendrez, great question. I have no idea unfortunately, you can open or fin an issue on github if you would like to discuss this issue with the developers
0

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.

1 Comment

It might not be necessary to fully disable the extension. See my answer. Let me know if this works, since I currently cannot test it myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.