By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

__import__ Query

kaarthikeyapreyan
100+
P: 107
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 ?.
Jan 5 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Jan 5 '09 #2

kaarthikeyapreyan
100+
P: 107
@bvdet
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.
Jan 6 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.