Mechanic Mechanic - 1 year ago 135
Python Question

Error: IOStream has no fileno - SUMO

I am trying to run SUMO through traci interface. I copy pasted this example from this link. The code is as follows

import os, sys
import subprocess

if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.exit("please declare environment variable 'SUMO_HOME'")

PORT = 8813
sumoBinary = "C:/Program Files (x86)/DLR/Sumo/bin/sumo-gui"
sumoProcess = subprocess.Popen([sumoBinary, "-c", "example.sumocfg", \
"--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)

import traci
import traci.constants as tc

traci.vehicle.subscribe(vehID, (tc.VAR_ROAD_ID, tc.VAR_LANEPOSITION))

for step in range(3):
print("step", step)


When I try to run the code, it throws me the following error

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\", line 685, in runfile
execfile(filename, namespace)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/Users/Raja/Documents/vehicomPhd/SUMOTraffic/", line 22, in <module>
"--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)

File "C:\Anaconda3\lib\", line 823, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)

File "C:\Anaconda3\lib\", line 1037, in _get_handles
c2pwrite = msvcrt.get_osfhandle(stdout.fileno())

File "C:\Anaconda3\lib\site-packages\IPython\kernel\zmq\", line 205, in fileno
raise UnsupportedOperation("IOStream has no fileno.")

UnsupportedOperation: IOStream has no fileno.

Anyone know what is wrong.

Answer Source

Looks like you're running in an ipython notebook. They have non-standard "standard" I/O streams that can't be used like a "true" file object (because they're really data queues, not pipes, so they don't have a file descriptor to use for low level I/O).

You can't use them with libraries (like subprocess) that perform low level I/O; the error is there to tell you this.. You'll need to use a real file-like object, possibly something as simple as sending output to a tempfile.TemporaryFile and then copying output from the file to stdout if that's what you need.

It's possible it would work by just not passing Popen stdout and stderr arguments at all; the default behavior for subprocess is to use the same stdout and stderr as the parent, so if there are valid file handles opened (even if the notebook replaced sys.stdout/sys.stderr for Python use), it might "just work" (where "just work" includes the possibility that data sent to the underlying file descriptors 0 and 1 is discarded, so you never see it).

Or just don't run in the ipython notebook.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download