错误:IOStream没有fileno - SUMO

Mec*_*nic -1 python

我试图通过traci接口运行SUMO.我从这个链接复制粘贴这个例子.代码如下

import os, sys
import subprocess

if 'SUMO_HOME' in os.environ:
     tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
     sys.path.append(tools)
else:   
     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.init(PORT)
traci.vehicle.subscribe(vehID, (tc.VAR_ROAD_ID, tc.VAR_LANEPOSITION))
print(traci.vehicle.getSubscriptionResults(vehID))

for step in range(3):
    print("step", step)
    traci.simulationStep()
    print(traci.vehicle.getSubscriptionResults(vehID))

traci.close()
Run Code Online (Sandbox Code Playgroud)

当我尝试运行代码时,它会抛出以下错误

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

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

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

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

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

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

UnsupportedOperation: IOStream has no fileno.
Run Code Online (Sandbox Code Playgroud)

任何人都知道什么是错的.

Sha*_*ger 5

看起来你在ipython笔记本电脑上运行.它们具有非标准的"标准"I/O流,不能像"真正的"文件对象一样使用(因为它们实际上是数据队列,而不是管道,因此它们没有用于低的文件描述符等级I/O).

您不能将它们subprocess用于执行低级I/O的库(例如); 错误是告诉你的..你需要使用一个真正的文件类对象,可能就像将输出发送到a tempfile.TemporaryFile然后将文件输出复制到stdout你需要的那样简单.

它可能只是通过传递Popen stdoutstderr参数来工作; 默认行为subprocess是使用相同的stdoutstderr父对象,所以如果打开了有效的文件句柄(即使笔记本被替换sys.stdout/ sys.stderr用于Python),它可能"正常工作"(其中"只是工作"包括可能性数据发送到底层文件描述符0并被1丢弃,所以你永远不会看到它).

或者只是不要在ipython笔记本中运行.