PYTHONPATH and module names

Discussion in 'Python' started by Tobiah, Jul 1, 2013.

  1. Tobiah

    Tobiah Guest

    So today, I created a file called 'formatter.py',
    and my program broke. It turned out that I was
    also import 'gluon' from web2py, which in turn,
    somewhere, imported the regular python formatter.py
    with which I was not familiar.

    So the question is: Does one simply always have
    to be knowledgeable about existing python library
    names, or is having '.' in the python path just
    a bad idea? Is there a way, not having '.' in
    the path to explicitly specify the current directory?
    Something analogous to import ./foo ?

    Thanks,

    Tobiah
    Tobiah, Jul 1, 2013
    #1
    1. Advertising

  2. Tobiah

    rusi Guest

    On Monday, July 1, 2013 11:59:35 PM UTC+5:30, Tobiah wrote:
    > So today, I created a file called 'formatter.py',
    > and my program broke. It turned out that I was
    > also import 'gluon' from web2py, which in turn,
    > somewhere, imported the regular python formatter.py
    > with which I was not familiar.
    >
    > So the question is: Does one simply always have
    > to be knowledgeable about existing python library
    > names, or is having '.' in the python path just
    > a bad idea? Is there a way, not having '.' in
    > the path to explicitly specify the current directory?
    > Something analogous to import ./foo ?


    Are you familiar with absolute and relative imports:
    http://docs.python.org/release/2.5/whatsnew/pep-328.html
    rusi, Jul 1, 2013
    #2
    1. Advertising

  3. Tobiah

    Tobiah Guest

    > Are you familiar with absolute and relative imports:
    > http://docs.python.org/release/2.5/whatsnew/pep-328.html


    Doesn't seem to work:

    Python 2.7.3 (default, May 10 2012, 13:31:18)
    [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from __future__ import absolute_import
    >>> import .format

    File "<stdin>", line 1
    import .format
    ^
    SyntaxError: invalid syntax
    >>>
    Tobiah, Jul 1, 2013
    #3
  4. Relative imports only work with the "from ... import ..." form.

    — SpaghettiToastBook


    On Mon, Jul 1, 2013 at 3:54 PM, Tobiah <> wrote:
    >> Are you familiar with absolute and relative imports:
    >> http://docs.python.org/release/2.5/whatsnew/pep-328.html

    >
    >
    > Doesn't seem to work:
    >
    > Python 2.7.3 (default, May 10 2012, 13:31:18)
    > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> from __future__ import absolute_import
    >>>> import .format

    > File "<stdin>", line 1
    > import .format
    > ^
    > SyntaxError: invalid syntax
    >>>>

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    SpaghettiToastBook ., Jul 1, 2013
    #4
  5. Tobiah

    rusi Guest

    On Tuesday, July 2, 2013 1:24:30 AM UTC+5:30, Tobiah wrote:
    > > Are you familiar with absolute and relative imports:
    > > http://docs.python.org/release/2.5/whatsnew/pep-328.html

    >
    > Doesn't seem to work:
    > Python 2.7.3 (default, May 10 2012, 13:31:18)
    > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> from __future__ import absolute_import
    > >>> import .format

    > File "<stdin>", line 1
    > import .format
    > ^
    > SyntaxError: invalid syntax
    > >>>


    1. My reading of
    http://www.python.org/dev/peps/pep-0328/
    is that this only works for from statements not import statements.
    [See the section called Guido's decision]

    2. The __future__ is not necessary in python 2.7
    [Not necessary or not allowed I not know :) ]
    rusi, Jul 1, 2013
    #5
  6. On Mon, 01 Jul 2013 14:38:50 -0700, rusi wrote:

    > On Tuesday, July 2, 2013 1:24:30 AM UTC+5:30, Tobiah wrote:
    >> > Are you familiar with absolute and relative imports:
    >> > http://docs.python.org/release/2.5/whatsnew/pep-328.html

    >>
    >> Doesn't seem to work:
    >> Python 2.7.3 (default, May 10 2012, 13:31:18) [GCC 4.2.4 (Ubuntu
    >> 4.2.4-1ubuntu4)] on linux2 Type "help", "copyright", "credits" or
    >> "license" for more information.
    >> >>> from __future__ import absolute_import import .format

    >> File "<stdin>", line 1
    >> import .format
    >> ^
    >> SyntaxError: invalid syntax
    >> >>>
    >> >>>

    > 1. My reading of
    > http://www.python.org/dev/peps/pep-0328/ is that this only works for
    > from statements not import statements. [See the section called Guido's
    > decision]



    Correct. This would have to be written as:

    from . import format


    but note that this only work in a package, not from some arbitrary module
    inside a directory.



    > 2. The __future__ is not necessary in python 2.7 [Not necessary or not
    > allowed I not know :) ]


    Not necessary.

    __future__ statements are guaranteed to "work" in all future versions, in
    the sense that once a __future__ feature is added, it will never be
    removed. So Python has had "nested scopes" since version 2.2 (by memory),
    but:

    from __future__ import nested_scopes

    still is allowed in Python 3.3, even though it has been a no-op since 2.2
    or 2.3.


    --
    Steven
    Steven D'Aprano, Jul 1, 2013
    #6
  7. On 1 Jul 2013 20:58, "Tobiah" <> wrote:
    >>
    >> Are you familiar with absolute and relative imports:
    >> http://docs.python.org/release/2.5/whatsnew/pep-328.html

    >
    >
    > Doesn't seem to work:
    >
    > Python 2.7.3 (default, May 10 2012, 13:31:18)
    > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> from __future__ import absolute_import
    > >>> import .format

    > File "<stdin>", line 1
    > import .format
    > ^
    > SyntaxError: invalid syntax
    > >>>


    Have you tried

    from . import format

    ?
    Fábio Santos, Jul 1, 2013
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fBechmann
    Replies:
    0
    Views:
    399
    fBechmann
    Jun 10, 2004
  2. Phd
    Replies:
    3
    Views:
    468
    Fredrik Lundh
    Dec 14, 2004
  3. wanwan
    Replies:
    3
    Views:
    427
    Alex Martelli
    Oct 14, 2005
  4. Andreas Otto
    Replies:
    6
    Views:
    768
    Andreas Otto
    May 21, 2009
  5. Stuart Moffatt

    PYTHONPATH issue with sibling package names

    Stuart Moffatt, Sep 10, 2009, in forum: Python
    Replies:
    2
    Views:
    494
    Stuart Moffatt
    Sep 10, 2009
Loading...

Share This Page