#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    What's the difference between relative imports and absolute imports in Python?


    Also, what does this special '.' or '..' or '...' syntax do while importing modules?

    Code:
    from . import spam #or
    from .spam import name #or
    from .. import string
    Thanks, in advance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by b49P23TIvg
    I didn't get the thing which I wanted to.
    Please, someone, for the love of god, just explain to me the '.' syntax in Python.
    from . import hello #or
    from .. import jello # or
    from . spam import whatthehell
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    The dot syntax is for using relative imports within a package. If you aren't creating a package then it isn't necessary to use. If however you are creating a package and one of your modules within said package needs to import another module within said package then:
    Code:
    from . import whatever
    would import the module whatever, where whatever was a module within the same directory as the module importing it.
    Code:
    from .. import whatever
    does the same thing except now whatever is in the directory above the importing module rather than the same directory. Now if you want a module that is in another directory within the directory your module is in then you would use:
    Code:
    from .other_directory import whatever
    If you are just starting out then you probably won't need to deal with any of this as you won't be creating packages any time soon.

    -Mek
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Mekire
    ....
    -Mek
    You know what? You're right. I won't use it, I have other techniques and I think that it rarely comes in practice.
    Thanks, anyway.

IMN logo majestic logo threadwatch logo seochat tools logo