Menu

#1 OS X: ImportError: cannot import name Supervisor

1.0
closed
nobody
None
2015-01-10
2015-01-08
vanyatka
No

Running simulator on OS X gives the following error (shown in a QT modal dialog window):

Traceback (most recent call last):

  File "./scripts/simulator.py", line 576, in __process_queue
    self.__class__.__dict__[name](self,*args)

  File "./scripts/simulator.py", line 94, in read_config
    self.load_world(world)

  File "./scripts/simulator.py", line 102, in load_world
    self.__construct_world()

  File "./scripts/simulator.py", line 142, in __construct_world
    sup_class = helpers.load_by_name(thing.supervisor.type,'supervisors')

  File "./scripts/helpers.py", line 111, in load_by_name
    module = __import__(path, globals(), locals(), [filename]).__dict__[filename]

  File "pysimiam-coursera-week7/supervisors/week7.py", line 9, in <module>
    from supervisors.quickbot import QuickBotSupervisor

  File "pysimiam-coursera-week7/supervisors/quickbot.py", line 9, in <module>
    from supervisor import Supervisor

ImportError: cannot import name Supervisor

$ python
Python 2.7.9 (default, Dec 30 2014, 18:33:32)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin

Discussion

  • Tim Fuchs

    Tim Fuchs - 2015-01-08

    Hm, that's weird. Just to make sure the environment is sane:

    • you are running 'qtsimiam_week7.py' ?
    • the file scripts/supervisor.py exists and contains class Supervisor ?
     
    • vanyatka

      vanyatka - 2015-01-08

      Hi Tim,

      Thanks for the prompt reply. See my comments inline.

      you are running 'qtsimiam_week7.py' ?

      Yes. But, is there more than one way to run the application?

      the file scripts/supervisor.py exists and contains class Supervisor

      pysimiam-coursera-week7$ cat -n scripts/supervisor.py | sed '2,10!d'
           2  import helpers
           3
           4  class Supervisor:
           5      """
           6          The supervisor class oversees the control of a single robot.
           7          The supervisor does not move the robot directly. Instead, the supervisor
           8          selects a controller to do the work and uses the controller outputs
           9          to generate the robot inputs.
          10
      

      or

      pysimiam-coursera-week7$ md5 scripts/supervisor.py
      MD5 (scripts/supervisor.py) = 0383a6b1ca24f3cc36efc1849c320fb5
      

      Quick search of the error messages gets me to this SO thread, could it be related?

      http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x

       
      • Tim Fuchs

        Tim Fuchs - 2015-01-08

        This will be interesting.

        The error that you are seeing means that although Python has found a file "supervisor.py", there is nothing named "Supervisor" in it. Since we have established that there is actually a class named Supervisor in scripts/supervisor.py, this seems to indicate that there is another file supervisor.py in path that gets found before the correct one.

        qtsimiam_week7.py adds the "scripts" folder to the path, so it is important that it is run somehow (I don't think there are many ways to run it - command-line or Finder). But this is being done already.

        Can you check if there are any other supervisor.py files in the pysimiam folder and subfolders? For example in "supervisors/"? If there are, please rename them to something else. If there is only scripts/supervisors.py, then we have to check the path. Please add

        print sys.path
        

        in the beginning of load_by_name method in scripts/helpers.py and check what is printed in the terminal. Mine looks like this:

        ['./scripts', './gui', './scripts', '/home/timoty/Compiling/pysimiam-coursera-week7', '/usr/lib64/python2.7/site-packages/pycallgraph-1.0.1-py2.7.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode']
        
         
        • vanyatka

          vanyatka - 2015-01-08

          Can you check if there are any other supervisor.py files in the pysimiam folder and subfolders?

          Well, I did not add or remove any files from the original package.

          pysimiam-coursera-week7$ find . | grep supervisor.py
          ./scripts/supervisor.py
          

          Here is the output after adding sys.path as you suggested:

          pysimiam-coursera-week7$ ./qtsimiam_week7.py
          Simulator: starting simulator thread
          QMenu: No OSMenuRef created for popup menu
          Simulator: Loading new world
          ['./scripts', './gui', './scripts', '/Users/ibalashov/Dropbox/courses/conrob-002/pysimiam/pysimiam-coursera-week7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-9.1-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-6.0.3-py2.7.egg', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/usr/local/lib/python2.7/site-packages/setuptools-9.1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/pip-6.0.3-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages']
          ['./scripts', './gui', './scripts', '/Users/ibalashov/Dropbox/courses/conrob-002/pysimiam/pysimiam-coursera-week7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-9.1-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-6.0.3-py2.7.egg', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/usr/local/lib/python2.7/site-packages/setuptools-9.1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/pip-6.0.3-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages']
          Module week7 failed to load
          Simulator: [Simulator.construct_world] Robot creation failed!
          2015-01-09 00:47:00.686 Python[6179:d0b] modalSession has been exited prematurely - check for a reentrant call to endModalSession:
          Simulator: stopping simulator thread
          

          I must say that this is only OS X issue. AFAIR, on Linux the simulator works fine, however right now I don't have it at hand to run and compare.

           
          • Tim Fuchs

            Tim Fuchs - 2015-01-08

            The problem is that not only is this a Mac OS X-specific issue, it also seems to depend on the particular setup, because I cannot reproduce it on my Mac (I am using python from Anaconda). I will think about it some more.

             
          • Tim Fuchs

            Tim Fuchs - 2015-01-09

            Please try the attached archive and tell me if it works.

             
            • vanyatka

              vanyatka - 2015-01-09

              Yes, I'm glad to say it's working now.

              Some namespace collisions caused the issue? Any idea why it didn't work only in my env? )

              In any case, thanks for all your help.

               
              • Tim Fuchs

                Tim Fuchs - 2015-01-10

                I'm still not sure why it didn't work on your machine - as I've said, python tried to import something different from what it should have. As you say, some namespace collision. All I did was to switch to fully qualified imports for the scripts folder, without playing with the sys.path, and it worked.

                Actually, I know what else we could have tried. The error message was

                  File "pysimiam-coursera-week7/supervisors/quickbot.py", line 9, in <module>
                    from supervisor import Supervisor
                
                ImportError: cannot import name Supervisor
                

                So you can try to replace

                from supervisor import Supervisor
                

                with

                import supervisor
                print supervisor.__file__
                

                This should tell us where the imported file is.

                 
                • vanyatka

                  vanyatka - 2015-01-10

                  Finally figured what was causing this.
                  I have a supervisor package installed in my system (installed via pip install supervisor).
                  It installs a system module with same name, which was imported instead of local.

                  Yeah, unique namespaces are great way avoid such issues.
                  Thanks for you help!

                   
                  • Tim Fuchs

                    Tim Fuchs - 2015-01-10

                    Thank you for reporting the bug and finding the cause. Apparently I didn't quite understand how the import mechanism works - sys.path seems to be the last place where it looks.

                    If you don't mind me asking, how come you have started with week7? Or did you do finish all other weeks earlier?

                     
                    • vanyatka

                      vanyatka - 2015-01-10

                      Yeah, I'm glad it's solved now. I got my lesson from this.

                      If you don't mind me asking, how come you have started with week7? Or did you do finish all other weeks earlier?

                      As probably most of the users of this simulator I started with matlab version, completed about 3-4 week and then realized that matlab is a bit difficult to work with, and python would be more dev friendly (ironically, it hadn't been until now). Now I have a chance to proceed from where I left off. And it looks like it was well worth the effort, the GUI looks great, and seems like debugger just works without matlab 'hacks'.

                       
                      • Tim Fuchs

                        Tim Fuchs - 2015-01-10

                        Then good luck with week 7. Please let me know if you encounter any other problems.

                         
  • Tim Fuchs

    Tim Fuchs - 2015-01-10
    • status: open --> closed
     

Log in to post a comment.