1 Ubuntu系统下
https://blog.csdn.net/renguoqing1001/article/details/52743255
本系统为ubuntu 17.10 采用 sudo apt-get install sumo sumo-tools安装sumo
这样的问题是 系统没有SUMO_HOME环境变量,在按照官网的教程中无法寻找到相关的环境变量。
因此在 写代码之前先加入SUMO_HOME这个环境变量。
第一步: Ubuntu17.10系统下加入SUMO_HOME环境变量
exprot SUMO_HOME= '/usr/share/sumo/tools'
第二步:
import sys,os,subprocess sys.path.append('/usr/share/sumo/tools') #import traci,sumolib sumoBinary = "/usr/bin/sumo" #sumo or sumo-gui connection import traci
第三步:
PORT=8813 sumoProcess = subprocess.Popen([sumoBinary , "-c", "sumo_config/heping.sumocfg", \ # "--remote-port", str(PORT), "--xml-validation", "never"],stdout=sys.stdout, stderr=sys.stderr, ) traci.init(self.PORT) ....... traci.close()
2 Windows平台下:
参考:http://www.sumo.dlr.de/userdoc/TraCI/Interfacing_TraCI_from_Python.html
第一步:准备sumo-home环境变量并导入相关模块:
import sys,os # sumo related modules try: sys.path.append(os.path.join(os.path.dirname( __file__), '..', '..', '..', '..', "tools")) # tutorial in tests sys.path.append(os.path.join(os.environ.get("SUMO_HOME", os.path.join( os.path.dirname(__file__), "..", "..", "..")), "tools")) # tutorial in docs except ImportError: sys.exit( "please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')") sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools")) import traci import sumolib
第二步: python和traci串口通信 开启
sumoBinary = "/path/to/sumo-gui" # 此处sumo-gui 和 sumo按照需求使用 sumoCmd = [sumoBinary, "-c", "yourConfiguration.sumocfg"] traci.start(sumoCmd)