Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4942
| From | "John O'Hagan" <research@johnohagan.com> |
|---|---|
| Organization | johnohagan.com |
| Subject | Re: Python3: imports don't see files from same directory? |
| Date | 2011-05-08 06:04 +0000 |
| References | <ce234ef9-34c1-4fbc-a064-52dce1cef240@p23g2000vbl.googlegroups.com> <BANLkTinmp5U9siuh6Y+08=GxKW9dZCTBzw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1303.1304834694.9059.python-list@python.org> (permalink) |
On Sat, 7 May 2011, Ian Kelly wrote:
[...]
>
> 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?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
Python3: imports don't see files from same directory? dmitrey <dmitrey15@gmail.com> - 2011-05-07 03:02 -0700 Re: Python3: imports don't see files from same directory? Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-07 09:07 -0600 Re: Python3: imports don't see files from same directory? "John O'Hagan" <research@johnohagan.com> - 2011-05-08 06:04 +0000 Re: Python3: imports don't see files from same directory? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-05-07 23:24 -0700
csiph-web