Py3: Import relative path module

Gnarlodious gnarlodious at gmail.com
Thu Nov 4 02:38:14 CET 2010


> Peter Otten wrote:

> |-- alpha
> |   |-- __init__.py
> |   |-- beta
> |   |   |-- __init__.py
> |   |   `-- one.py
> |   `-- two.py
> `-- main.py
>
> 2 directories, 5 files
> $ cat main.py
> import alpha.beta.one
> $ cat alpha/beta/one.py
> from ..alpha import two
> $ cat alpha/two.py
> print "success!"
> $ python main.py
> success!

Thank you for that example, it works for me. However, what I want to
do is go UP one folder to import the module(s) in some library:

python main.py
    import ..alpha.beta.one
           ^
SyntaxError: invalid syntax

python main.py
    from .. import alpha.beta.one
                        ^
SyntaxError: invalid syntax

So how to specify a relative path?

-- Gnarlie



More information about the Python-list mailing list