Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Relative imports in Python 3.0

Reply
Thread Tools

Relative imports in Python 3.0

 
 
Nicholas
Guest
Posts: n/a
 
      12-17-2008
Imagine a module that looks like

ModuleDir
__init__.py
a.py
b.py


In python 2.x I used to have tests at the end of each of my modules,
so that module b.py might look something like

import a
..........
..........

if __name__ == '__main__':
runtests()

But under Python 3.0 this seems impossible. For usual use import a.py
has to become the line:

from . import a

But if I use that form it is no longer possible to run b.py as a
standalone script without raising an error about using relative
imports.

I am sure I am not the first to run into this issue, but what is the
solution?

Best wishes,

Nicholas
 
Reply With Quote
 
 
 
 
Brian Allen Vanderburg II
Guest
Posts: n/a
 
      12-17-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Imagine a module that looks like
>
> ModuleDir
> __init__.py
> a.py
> b.py
>
>
> In python 2.x I used to have tests at the end of each of my modules,
> so that module b.py might look something like
>
> import a
> ..........
> ..........
>
> if __name__ == '__main__':
> runtests()
>
> But under Python 3.0 this seems impossible. For usual use import a.py
> has to become the line:
>
> from . import a
>
> But if I use that form it is no longer possible to run b.py as a
> standalone script without raising an error about using relative
> imports.
>
> I am sure I am not the first to run into this issue, but what is the
> solution?
>
> Best wishes,
>
> Nicholas
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Sorry for the duplicate, sent to wrong email.

Python 3 (and I think 2.6) now use absolute import when using a 'import
blah' statement.

if ('.' in __name__) or hasattr(globals, '__path__'):
from . import a
else:
import a

If '__name__' has a'.' then it is either a package or a module in a
package, in which case relative imports can be used. If it does not
have a '.' it may still be a package but the '__init__.py' file, in
which case the module has a '__path__' attribute, so relative imports
can be used. Otherwise it is not a package or in a package so absolute
imports must used. Also, since it is not in a package it is assumed
that it is top module (__main__) or possible module imported from the
top that is not in a package, such as a.py doing an 'import b', b would
be a module but not a package so still probably need absolute imports,
my guess anyway.

But I also think that 'from . import a' would be nice if it would work
from non-packages as well, meaning just 'import a' if it is a non-package.

Brian A. Vanderburg II
 
Reply With Quote
 
 
 
 
Benjamin
Guest
Posts: n/a
 
      12-17-2008
On Dec 17, 4:01*am, Nicholas <(E-Mail Removed)> wrote:
> Imagine a module that looks like
>
> ModuleDir
> * * *__init__.py
> * * *a.py
> * * *b.py
>
> In python 2.x I used to have tests at the end of each of my modules,
> so that module b.py might look something like
>
> import a
> *..........
> *..........
>
> if __name__ == '__main__':
> * *runtests()
>
> But under Python 3.0 this seems impossible. *For usual use import a.py
> has to become the line:
>
> from . import a
>
> But if I use that form it is no longer possible to run b.py as a
> standalone script without raising an error about using relative
> imports.
>
> I am sure I am not the first to run into this issue, but what is the
> solution?


Use absolute imports:

from ModuleDir import a

>
> Best wishes,
>
> Nicholas


 
Reply With Quote
 
Kay Schluehr
Guest
Posts: n/a
 
      12-18-2008
On 17 Dez., 11:01, Nicholas <(E-Mail Removed)> wrote:

> I am sure I am not the first to run into this issue, but what is the
> solution?


When you use 2to3 just uncomment or delete the file fix_import.py in
lib2to3/fixes/ .

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
help with relative imports John Salerno Python 5 09-19-2006 07:20 PM
Relative imports Chris Python 4 03-05-2005 03:34 PM
Writing apps without using relative imports Randall Smith Python 0 12-03-2004 12:30 AM



Advertisments