0

it runs perfectly in CMD, or the VScode terminal, but with GitBash it crashes on line 30

anyone could tell me why ?

[heres my program, a Python IRC program]

import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = 'irc.HOST.org'
PORT = 6667

s.connect((HOST, PORT))

nick_cr = ('NICK thewok \r\n').encode()
s.send(nick_cr)
username_cr= ('USER thewok thewok thewok :thewok \r\n').encode()
s.send(username_cr)
time.sleep(2)
s.send('JOIN #CHANNEL \r\n'.encode()) #chanel

while 1:
    data = s.recv(4096).decode('utf-8')
    print(data) #that line is causing problems,
    #in GitBash, it says :
    #line 19, in this .py,
    #print(data)
    #File "C:\Python39\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 1456-1465: character maps to <undefined>



    if data.find('PING') != -1: #some irc actions
        s.send(str('PONG ' + data.split(':')[1] + '\r\n').encode())
        print('PONG sent \n')

    if data.find("PRIVMSG") != -1: #some irc actions
        final_result = "PRIVMSG...\n"
        s.send(final_result.encode())
                
    else:
        time.sleep(5) #some irc actions
        send_nude = "PRIVMSG Candy !ep1" + "\n"
        s.send(send_nude.encode())
        time.sleep(0.5)

why does it gives me that UnicodeEncodeError while it runs absolutely fine in anything else ?

  • 1
    Likely your paths are configured in such a way that Git Bash is running a different python than everything else. `which python` will tell you what your bash python interpreter is, compare that to your vscode python interpreter and see if they're different – Kaia Jan 24 '23 at 21:09
  • 1
    Show the full traceback of the error as properly formatted text in the question. – Michael Butscher Jan 24 '23 at 21:10
  • Welcome to Stack Overflow! Please take the [tour]. It looks like someone's already asked about this problem, so I've closed your question accordingly. But if none of the answers solve your problem, [edit] your question and explain what happened when you tried them, and we can reopen it. Check out [ask] for tips, like starting with your own research (for example I googled `gitbash python print UnicodeEncodeError` to find that question). – wjandrea Jan 24 '23 at 21:18
  • 2
    One addendum to @Kaia's generally good advice -- `type python` is more appropriate than `which python` in bash; `which` doesn't know about aliases, shell functions, aliased/cached lookups, etc. – Charles Duffy Jan 24 '23 at 21:19

0 Answers0