Python3: imports don't see files from same directory?

I

Ian Kelly

hi all,
I try to port my code to Python 3 and somehow files don't see files
from same directory, so I have to add those directories explicitly,
e.g.
import sys
sys.path += [...]

Also, it leads to bugs like this one:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/961a90219a61e19d#

any ideas what's the reason and how to fix it?
I have tried to search google but got nothing yet.

Implicit relative imports were removed in Python 3 to prevent
ambiguity as the number of packages grows. See PEP 328.

If you have two modules in the same package, pack1.mod1 and
pack1.mod2, then in pack1.mod1 you can no longer just do "import mod2"
or "from mod2 import foo". Either use an absolute import ("from
pack1.mod2 import foo") or make the relative import explicit ("from
..mod2 import foo" -- note the ".")

If you're upgrading scripts from Python 2 to Python 3, you should
really run them through the 2to3 tool. I believe this is one of the
many things it will fix for you automatically.
 
J

John O'Hagan

]
Implicit relative imports were removed in Python 3 to prevent
ambiguity as the number of packages grows. See PEP 328.

If you have two modules in the same package, pack1.mod1 and
pack1.mod2, then in pack1.mod1 you can no longer just do "import mod2"
or "from mod2 import foo". Either use an absolute import ("from
pack1.mod2 import foo") or make the relative import explicit ("from
.mod2 import foo" -- note the ".")

If you're upgrading scripts from Python 2 to Python 3, you should
really run them through the 2to3 tool. I believe this is one of the
many things it will fix for you automatically.

For some reason I haven't fathomed yet, I've found that while 2to3
does change the import syntax to the dot form as you say, this results
in "ValueError: Attempted relative import in non-package", and I have
to change it back to the old way, which works fine although the docs
say it shouldn't. This is python 3.2 on Debian testing.

For example, if I have a directory containing an __init__.py file, and two
modules, one of which is called mod1 and contains

#!/usr/bin/python3
a=1

in the other module I can have

import mod1

or

from mod1 import a

but not

from .mod1 import a

or

import .mod1


What gives?
 
B

Benjamin Kaplan

]
Implicit relative imports were removed in Python 3 to prevent
ambiguity as the number of packages grows.  See PEP 328.

If you have two modules in the same package, pack1.mod1 and
pack1.mod2, then in pack1.mod1 you can no longer just do "import mod2"
or "from mod2 import foo".  Either use an absolute import ("from
pack1.mod2 import foo") or make the relative import explicit ("from
.mod2 import foo" -- note the ".")

If you're upgrading scripts from Python 2 to Python 3, you should
really run them through the 2to3 tool.  I believe this is one of the
many things it will fix for you automatically.

For some reason I haven't fathomed yet, I've found that while 2to3
does change the import syntax to the dot form as you say, this results
in "ValueError: Attempted relative import in non-package", and I have
to change it back to the old way, which works fine although the docs
say it shouldn't. This is python 3.2 on Debian testing.

For example, if I have a directory containing an __init__.py file, and two
modules, one of which is called mod1 and contains

#!/usr/bin/python3
a=1

in the other module I can have

import mod1

or

from  mod1 import a

but not

from .mod1 import a

or

import .mod1


What gives?

* Absolute imports are given as modules coming straight from something
on sys.path. If you actually check sys.path, you'll probably notice
that '' is on there, which in this case refers to the current
directory.

* In order to do relative imports, you need to be in a package. Having
an __init__.py somewhere does not automatically make it a package. To
be a package, it has to be in one of the folders on sys.path. Since
python doesn't have a name for that folder that __init__.py is in,
it's not actually a part of a package.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top