kboghe / NordVPN-switcher

Rotate between different NordVPN servers with ease. Works both on Linux and Windows without any required changes to your code!
https://pypi.org/project/nordvpn-switcher/
189 stars 51 forks source link

UnicodeEncodeError when running via terminal in windows #54

Open eranga-mohotty opened 5 months ago

eranga-mohotty commented 5 months ago

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:

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
  2. 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}")