FAQ
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)

Search Discussions

  • Chris Rebert at Mar 6, 2009 at 9:39 am

    On Fri, Mar 6, 2009 at 1:33 AM, TP wrote:
    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

    --
    I have a blog:
    http://blog.rebertia.com

    From http Fri Mar 6 10:48:53 2009
    From: http (Paul Rubin)
    Date: 06 Mar 2009 01:48:53 -0800
    Subject: Can Python do shopping cart?
    References: <22430[email protected]>
    Message-ID: <[email protected]>

    Muddy Coder <cosmo_general at yahoo.com> writes:
    I know PHP can do shopping cart, such as Zen Cart. I wonder can Python
    do such a thing? Thanks!
    As they say in a certain other place--the answer is: yes! Python can
    do that.
  • Ben Finney at Mar 6, 2009 at 9:54 am
    (Could you please set a valid email address for people to contact you
    if necessary?)

    TP <Tribulations at Paralleles.invalid> writes:
    I would like to prevent the loading of modules in the current
    directory.
    You will, I think, like the new distinction between ?absolute? and
    ?relative? imports <URL:http://www.python.org/dev/peps/pep-0328/>.

    --
    \ ?All my life I've had one dream: to achieve my many goals.? |
    `\ ?Homer, _The Simpsons_ |
    _o__) |
    Ben Finney
  • TP at Mar 6, 2009 at 11:29 am

    Ben Finney wrote:

    (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)
  • Carl Banks at Mar 7, 2009 at 5:34 am

    On Mar 6, 1:54?am, Ben Finney wrote:
    (Could you please set a valid email address for people to contact you
    if necessary?)

    TP <Tribulati... at Paralleles.invalid> writes:
    I would like to prevent the loading of modules in the current
    directory.
    You will, I think, like the new distinction between ?absolute? and
    ?relative? imports <URL:http://www.python.org/dev/peps/pep-0328/>.
    He might, but it won't help him with his problem. Relative imports
    aren't involved in this situation.


    Carl Banks
  • Christian Heimes at Mar 6, 2009 at 10:19 am

    TP schrieb:
    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?
    Do you have the 'os' module inside your 'personal' package? If you add

    from __future__ import absolute_import

    then

    import os

    imports the right 'os' module. With

    from . import os

    you can import the 'personal.os' module from a module inside 'personal'.

    Christian
  • Benjamin Peterson at Mar 6, 2009 at 9:40 pm
    TP <Tribulations <at> Paralleles.invalid> writes:
    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.
  • TP at Mar 6, 2009 at 9:47 pm

    Benjamin Peterson wrote:

    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)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 6, '09 at 9:33a
activeMar 7, '09 at 5:34a
posts8
users6
websitepython.org

People

Translate

site design / logo © 2023 Grokbase