1

I have the following part of my code where I have a problem:

import os, sys
import optparse
import subprocess
import random

# we need to import python modules from the $SUMO_HOME/tools directory
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
    from sumolib import checkBinary
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')")

import traci

I declared SUMO_HOME as a system variable, but when I run this script I get the ImportError. Do you have any idea what the problem is?

11
  • 2
    use os.environ.get('SUMO_HOME'), not os.environ.get('$SUMO_HOME'). Commented Aug 21, 2014 at 20:48
  • I did it, no changes. Still the same error Commented Aug 21, 2014 at 20:49
  • How did you declare it? Commented Aug 21, 2014 at 20:50
  • In system variables as directory to folder with files D:\Folder1\Folder2 Commented Aug 21, 2014 at 20:52
  • 1
    @KrzysiekNowakowski Does printing the value of os.environ.get('SUMO_HOME') in the script display the correct path? Commented Aug 21, 2014 at 20:55

3 Answers 3

1

Why have you added a $? The name of the variable is SUMO_HOME, not $SUMO_HOME.

Sign up to request clarification or add additional context in comments.

1 Comment

As i said moment ago i changed it to SUMO_HOME but it still don't work.
1

I had some problems with it too. If I echo'ed in my terminal it showed the right path but within the python file it somehow would not find it. For me it was fixed by modifying the try-except block to:

# we need to import python modules from the $SUMO_HOME/tools directory
try:
    sys.path.append(<Path to tools>)
    from sumolib import checkBinary
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')")

So in my example I used this block and replaced <Path to tools> with '/Users/Isabelle/sumo-0.28.0/tools'

I know it is a late comment and you've probably solved it or moved on, but hopefully it will still help others!

Kind regards,

Isabelle

Comments

0

This solution might only be for MAC OS. Not sure about other OS. I've found what was wrong, I don't know why but there is a mistake on the number of '..' in the python code. You need to remove one. There need to be 3, not 4. It should be:

sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', "tools")) # tutorial in tests

instead. The first '..' brings you inside the 'tutorial' folder, the second inside the 'docs' folder and the third inside the main folder where 'bin', 'tools' and 'docs' are located.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.