__import__ Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaarthikeyapreyan
    New Member
    • Apr 2007
    • 106

    __import__ Query

    Query about the __import__ function

    Code:
    ~pwd
    /home/preyan/test
    ~ls
    sample.py
    ~python
    >>> mod=__import__('sample')
    >>> amod=__import__('/home/preyan/test/sample')
    >>>
    both the imports were fine but, when i did this

    Code:
    # /home/preyan/mypro.py
    mod='sample'
    os.chdir('/home/preyan/test')
    module=__import__(mod)
    
    Traceback (most recent call last):
      File "/home/preyan/mypro.py", line 3, in <module>
        module=__import__(mod)
    ImportError: No module named sample
    my python version
    Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    why is that am not able to import ?.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I don't know why you are getting the ImportError. Function __import__() is a low-level interface to the module loader, and does not perform all the steps performed by an import statement. The local namespace is not updated with names referring to objects contained within the module.
    Code:
    import foo
    # is equivalent to
    __import__('foo', globals(), locals(), [])
    HTH

    Comment

    • kaarthikeyapreyan
      New Member
      • Apr 2007
      • 106

      #3
      ambiguity

      Originally posted by bvdet
      I don't know why you are getting the ImportError. Function __import__() is a low-level interface to the module loader, and does not perform all the steps performed by an import statement. The local namespace is not updated with names referring to objects contained within the module.
      Code:
      import foo
      # is equivalent to
      __import__('foo', globals(), locals(), [])
      HTH
      I have tried the above statement too but in vain.
      the problem here is that if the interpreter is able to import it when its in the command line why is it not able to do it when it is inside a script.

      Comment

      Working...