Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45456 > unrolled thread

How to use relative Import

Started byChitrank Dixit <chitrankdixit@gmail.com>
First post2013-05-17 01:35 +0530
Last post2013-05-16 23:39 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  How to use relative Import Chitrank Dixit <chitrankdixit@gmail.com> - 2013-05-17 01:35 +0530
    Re: How to use relative Import Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-16 23:39 +0000

#45456 — How to use relative Import

FromChitrank Dixit <chitrankdixit@gmail.com>
Date2013-05-17 01:35 +0530
SubjectHow to use relative Import
Message-ID<mailman.1777.1368734751.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello Python developers

I want to know how relative imports are different than import. I have found
lots of examples on internet explaining about the relative import

but I want to know about the basic aspects of relative import that make it
different than import.


*Regards
*
*Chitrank Dixit
*
*IIPS-DAVV
*
*Indore (M.P.) , India *
*MCA
*
*trackleech.blogspot.in*

[toc] | [next] | [standalone]


#45460

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-16 23:39 +0000
Message-ID<51956e35$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#45456
On Fri, 17 May 2013 01:35:49 +0530, Chitrank Dixit wrote:

> I want to know how relative imports are different than import. I have
> found lots of examples on internet explaining about the relative import
> but I want to know about the basic aspects of relative import that make 
> it different than import.

Relative imports search the directory containing the current package, 
absolute imports search the entire contents of sys.path. That is the only 
difference.

The purpose of relative imports is to make it easy for modules inside a 
package to import each other without worrying about other modules 
*outside* the package being imported by mistake. You can read the PEP 
here:

http://www.python.org/dev/peps/pep-0328/

to learn about the motivation for absolute and relative imports.

Optional in Python 2.5 and 2.6, and compulsory in 2.7, when you write:

import moduleX

Python looks at sys.path for a list of directories to search for 
"moduleX.py". This is an absolute import.

But consider a package with this structure:

package/
  +--  __init__.py
  +--  moduleA.py
  +--  moduleB.py
  +--  moduleX.py
  +--  subpackage/
       +--  __init__.py
       +--  moduleY.py
       +--  moduleZ.py


Inside moduleA, if you write "import moduleX" it will be an absolute 
import, the entire sys.path will be searched, and the "top level" 
module.X will be found.

But if you write this instead:

from . import moduleX

you have a relative import, and Python will only search the current 
package directory, so it will find the moduleX inside the package.

You can also write:

from .subpackage import moduleZ

to find the moduleZ inside the subpackage, again without searching the 
entire sys.path.

Inside moduleY in the subpackage, all of these will find the same moduleZ:

# Relative to the current module:
from . import moduleZ

# Relative to the parent of the current module:
from ..subpackage import moduleZ
from .. import subpackage.moduleZ

# Absolute:
import package.subpackage.moduleZ



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web