Connecting Tech Pros Worldwide Forums | Help | Site Map

__import__ Query

kaarthikeyapreyan's Avatar
Member
 
Join Date: Apr 2007
Location: India
Posts: 101
#1: Jan 5 '09
Query about the __import__ function

Expand|Select|Wrap|Line Numbers
  1. ~pwd
  2. /home/preyan/test
  3. ~ls
  4. sample.py
  5. ~python
  6. >>> mod=__import__('sample')
  7. >>> amod=__import__('/home/preyan/test/sample')
  8. >>>
both the imports were fine but, when i did this

Expand|Select|Wrap|Line Numbers
  1. # /home/preyan/mypro.py
  2. mod='sample'
  3. os.chdir('/home/preyan/test')
  4. module=__import__(mod)
  5.  
  6. Traceback (most recent call last):
  7.   File "/home/preyan/mypro.py", line 3, in <module>
  8.     module=__import__(mod)
  9. 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's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: Jan 5 '09

re: __import__ Query


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.
Expand|Select|Wrap|Line Numbers
  1. import foo
  2. # is equivalent to
  3. __import__('foo', globals(), locals(), [])
HTH
kaarthikeyapreyan's Avatar
Member
 
Join Date: Apr 2007
Location: India
Posts: 101
#3: Jan 6 '09

re: __import__ Query


Quote:

Originally Posted by bvdet View Post

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.

Expand|Select|Wrap|Line Numbers
  1. import foo
  2. # is equivalent to
  3. __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.
Reply