how to prevent python import from looking into the current directory

T

TP

Hi everybody,

I would like to prevent the loading of modules in the current directory.
For example, if I have a personal module in the current directory
named "os", when I do "import os", I would like Python to import os
standard module, not my personal module of the current directory.
Is this possible?

Thanks
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
C

Chris Rebert

Hi everybody,

I would like to prevent the loading of modules in the current directory.
For example, if I have a personal module in the current directory
named "os", when I do "import os", I would like Python to import os
standard module, not my personal module of the current directory.
Is this possible?

This should do the trick:

import sys
sys.path.remove('')

Cheers,
Chris
 
T

TP

Ben said:
(Could you please set a valid email address for people to contact you
if necessary?)

Thanks a lot for your help.
My email address is in my signature:

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 
B

Benjamin Peterson

TP said:
Hi everybody,

I would like to prevent the loading of modules in the current directory.
For example, if I have a personal module in the current directory
named "os", when I do "import os", I would like Python to import os
standard module, not my personal module of the current directory.
Is this possible?

While the solutions given by others in this thread will work, I think it is best
policy to not name your own modules after stdlib ones. When I see "os"
referenced in code, I assume it is the stdlib one, and don't want to be confused
by the presence of your own module.
 
T

TP

Benjamin said:
While the solutions given by others in this thread will work, I think it
is best policy to not name your own modules after stdlib ones. When I see
"os" referenced in code, I assume it is the stdlib one, and don't want to
be confused by the presence of your own module.

Yes, I have finally chosen this alternative: things are much simpler like
this.
Thanks

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top