Under 1 Ubuntu system
https://blog.csdn.net/renguoqing1001/article/details/52743255
This system is employed ubuntu 17.10 sudo apt-get install sumo sumo-tools mounted sumo
This problem is not SUMO_HOME system environment variables, according to the official website of the tutorial can not find the relevant environmental variables.
So before you write the code to join SUMO_HOME this environment variable.
Join SUMO_HOME Ubuntu17.10 system environment variables: the first step
exprot SUMO_HOME= '/usr/share/sumo/tools'
Step two:
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
third step:
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 platforms:
Reference: http://www.sumo.dlr.de/userdoc/TraCI/Interfacing_TraCI_from_Python.html
Step 1: Preparation sumo-home environment variable and import related modules:
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 sumolib Imports Traci
Step two: python and traci serial communication open
= sumoBinary "/ path / to / sumo-gui" # sumo and sumo-gui here on demand using sumoCmd = [sumoBinary, "-C", "yourConfiguration.sumocfg"] traci.start (sumoCmd)