Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError when running via terminal in windows #54

Open
eranga-mohotty opened this issue Jan 21, 2024 · 0 comments
Open

UnicodeEncodeError when running via terminal in windows #54

eranga-mohotty opened this issue Jan 21, 2024 · 0 comments

Comments

@eranga-mohotty
Copy link

Issue:
I encountered a UnicodeEncodeError when running a python script that calls the function "initialize_VPN(area_input=['complete rotation'])" through a .bat file. note issue only exists when executing via .bat file and won't appear when running via an IDE like pyCharm or directly via terminal.

Error:
Traceback (most recent call last):
File "...\main.py", line 3, in
instructions = initialize_VPN(area_input=['complete rotation'])
File "...\ProjectFolder\venv\lib\site-packages\nordvpn_switcher\nordvpn_switch.py", line 142, in initialize_VPN
print("NordVPN installation check: \33[92m\N{check mark}\33[0m")
File "C:\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 character '\u2713' in position 33: character maps to

Environment:

  • Python version: [3.10]
  • Operating System: [Windows 10]
  • Terminal used: [Windows Terminal]

Steps to Reproduce:

  1. create python script main.py:
    Content of py file:
from nordvpn_switcher import initialize_VPN, rotate_VPN

instructions = initialize_VPN(area_input=['complete rotation'])
rotate_VPN(instructions)
  1. create bat file named "test.bat" inside the folder of a project created with pyCharm that has module nordvpn-switcher installed.
    Content of bat file:
@echo off
REM Replace "venv\Scripts\activate" with the actual path to your virtual environment
call venv\Scripts\activate
REM Run your Python script
python main.py
REM Deactivate the virtual environment
deactivate
pause
  1. Run "test.bat" via terminal
    .\vpn_switch.bat

Expected Behavior:
The script should run without any UnicodeEncodeError and complete the rotation as intended.

Actual Behavior:
The script raises a UnicodeEncodeError at the following line in nordvpn_switch.py:
python
print("NordVPN installation check: \33[92m\N{check mark}\33[0m")

Possible Fix: (fix only works for versions python 3.7 and above)
add following code to top of file nordvpn_switch.py

import sys


# Check if the platform is Windows
if sys.platform == 'win32':
    # Change the encoding to 'utf-8' for Windows terminal
    if sys.version_info >= (3, 7): 
        try:
            sys.stdout.reconfigure(encoding='utf-8') # minimum python version of 3.7 is required 
        except Exception as e:
            print(f"An error occurred while reconfiguring stdout: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant