Print error in pycharm

Hello,

I get the following error in Pycharm.

# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

The error is in the print command.

print >>sys.stderr, 'starting up on %s port %s' % server_address
    ~~~~~~^^~~~~~~~~~~
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?```

What is your question?

I am wondering how to fix the “print” command and to run the program without error.

The error message contains a suggested fix. Does it not work?

I change

print >>sys.stderr, 'starting up on %s port %s' % server_address

to

print (sys.stderr, 'starting up on %s port %s' % server_address)

and it works.

1 Like

Is your intention to print the repr of sys.stderr to sys.stdout? You probably meant print('starting up on %s port %s' % server_address, file=sys.stderr).

I change

# print (sys.stderr, 'starting up on %s port %s' % server_address)
to
print('starting up on %s port %s' % server_address, file=sys.stderr)

Still some errors. I will post the question.

It seems like you are following a tutorial designed for Python 2. Python 2 is quite old now and has significant incompatibilities with Python 3. I’d suggest using a more recent resource that uses Python 3.

2 Likes