Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Py3: Import relative path module

9 views
Skip to first unread message

Gnarlodious

unread,
Oct 31, 2010, 11:06:05 AM10/31/10
to
I am loathe to duplicate programming in files that should just load a
copy from a module. I tried all kinds of tricks to import a module
from one level up. What's the secret?

It works if I say:

from Data import DumpHT

but ONLY if the search path in sys.path. I want a relative path import
independent from sys.path.

How to? This is Python 3.1.1

-- Gnarlie

Дамјан Георгиевски

unread,
Oct 31, 2010, 1:09:42 PM10/31/10
to

from .. import Data.DumpHT as DumpHT


--
дамјан ((( http://damjan.softver.org.mk/ )))

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!

Gnarlodious

unread,
Nov 1, 2010, 4:08:52 AM11/1/10
to
On Oct 31, 11:09 am, Дамјан Георгиевски wrote:
> from .. import Data.DumpHT as DumpHT

That doesn't work. Any more ideas?

-- Gnarlie

Steven D'Aprano

unread,
Nov 1, 2010, 4:16:41 AM11/1/10
to

Define "doesn't work". Does it?


Print a warning message but continue execution?
Import the wrong module?
Abort Python without warning?
Dump core?
Reboot the operating system without warning?
Lock up the computer?
Something else?

--
Steven

Gnarlodious

unread,
Nov 1, 2010, 6:54:31 AM11/1/10
to
On Nov 1, 2:16 am, Steven D'Aprano <steve-REMOVE-

T...@cybersource.com.au> wrote:
> On Mon, 01 Nov 2010 01:08:52 -0700, Gnarlodious wrote:
> > On Oct 31, 11:09 am, Дамјан Георгиевски wrote:
> >> from .. import Data.DumpHT as DumpHT
>
> > That doesn't work. Any more ideas?
>
> Define "doesn't work".

LOL.
I get error:

from .. import Data.DumpHT as DumpHT

^
SyntaxError: invalid syntax

Rewording gets me closer:

from ..Data import DumpHT
ValueError: Attempted relative import in non-package

I do have the empty __init__.py file in the Data folder. What is
wrong?

-- Gnarlie


Peter Otten

unread,
Nov 1, 2010, 7:36:26 AM11/1/10
to
Gnarlodious wrote:

Remove the directory containing the importing file from your sys.path.
E. g. if you have

$ tree
.
`-- alpha
|-- __init__.py
|-- beta
| |-- __init__.py
| `-- one.py
`-- gamma
|-- __init__.py
`-- two.py

only the parent of alpha should be in your sys.path. Then to import
alpha/gamma/two.py from alpha/beta/one.py use

from ..gamma import two

Peter

Gnarlodious

unread,
Nov 1, 2010, 11:30:34 AM11/1/10
to
On Nov 1, 5:36 am, Peter Otten wrote:

> Remove the directory containing the importing file from your sys.path.

I removed all sys.path customizations and rebooted.

In the following scenario, I am programming in one.py attempting to
import Data.py which is in the alpha folder:


> $ tree
> .
> `-- alpha
>     |-- __init__.py
>     |-- beta
>     |   |-- __init__.py
>     |   `-- one.py
>     `-- gamma
>         |-- __init__.py
>         `-- two.py

However, all I can get is error:

from .. import Data


ValueError: Attempted relative import in non-package

Why is the parent folder not recognized as a package? Because right
now I am liberally using sys.path, which works but is a little too
messy.

-- Gnarlie

Peter Otten

unread,
Nov 1, 2010, 12:02:25 PM11/1/10
to
Gnarlodious wrote:

If you didn't add '/path/to/alpha' to your path explicitly then you may be
starting one.py as a script with

$ python /path/to/alpha/beta/one.py

one.py then becomes the __main__ module instead of alpha.beta.one.
Or your working directory is /path/to/alpha and you import one.py with

import beta.one

which makes beta instead of alpha the toplevel package.

Peter

Terry Reedy

unread,
Nov 1, 2010, 3:54:53 PM11/1/10
to pytho...@python.org

What about the folder above it?

As far as I know, the main reason to use relative imports is if you have
a subpackage that you expect to use within more then one package.
Otherwise, it is usually a lot easier to use absolute imports starting
with the top-level package.

--
Terry Jan Reedy


Gnarlodious

unread,
Nov 2, 2010, 9:48:37 PM11/2/10
to
I admit I don't understand any of what was said here. Or why it is so
hard what I am trying to do. I searched Google for a few hours looking
for a way to import a module from an absolute path, but that was a
waste of time too.

To reiterate, I am trying to import a Def function from a file one
level up. Let's say relative import is unreasonable to expect from
Python. If it is possible using an absolute path, please let me know
how, in plain language. Otherwise I can continue to copy and paste my
programming into several files.

Thank you for any help.

-- Gnarlie

MRAB

unread,
Nov 2, 2010, 10:29:30 PM11/2/10
to pytho...@python.org
After some experimentation (and Googling!) I think the problem is that
a module can do your relative import but a main script can't.

Or, to put it another way, if __name__ is "__main__" then it won't work.

Peter Otten

unread,
Nov 3, 2010, 4:51:46 AM11/3/10
to
MRAB wrote:

Slightly generalized: have the importing module print its __name__. There
has to be at least one dot in the name for

from .. import whatever

to succeed.

Gnarlodious

unread,
Nov 3, 2010, 9:05:43 AM11/3/10
to
On Nov 3, 2:51 am, Peter Otten wrote:

> Slightly generalized: have the importing module print its __name__. There
> has to be at least one dot in the name for
>
> from .. import whatever
>
> to succeed.

Just spent about 3 hours trying every permutation I could think of,
and searching Google for exactly how to do it, but all I get is:

ValueError: Attempted relative import in non-package

How do I import a module so that a dot will appear in its name?

-- Gnarlie

Peter Otten

unread,
Nov 3, 2010, 9:42:10 AM11/3/10
to
Gnarlodious wrote:

Make sure the no path in sys.path leads into a directory that contains an
__init__.py. In particular, ensure that you aren't invoking the python
interpreter from a working directory that contains an __init__.py and that
the main script is in a directory that doesn't contain an __init__.py.

Peter

Peter Otten

unread,
Nov 3, 2010, 9:50:45 AM11/3/10
to
Peter Otten wrote:

Here's a working example that you can use as a starting point:

$ tree
.
|-- 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!

Peter

Peter Otten

unread,
Nov 3, 2010, 10:02:34 AM11/3/10
to
Peter Otten wrote:

Hmm, now I'm puzzled myself.

> $ cat alpha/beta/one.py
> from ..alpha import two

That line should have been

from .. import two

For some reason (bug?) it seems to work with and without the extra alpha.

Peter

Gnarlodious

unread,
Nov 3, 2010, 9:38:14 PM11/3/10
to
> 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

Gnarlodious

unread,
Nov 3, 2010, 10:48:35 PM11/3/10
to
OK I've had a modicum of success! However I was forced to add the path
to my application folder to sys.path, which I suppose is the closest I
can get to what I want. The example given then works in the shell.

Apache is another problem. I added the same path to the script then
Apache finds the application folder and does the importing. Not an
ideal solution, but workable. I hope Apache3 in the future has more
smarts about importing relative filepaths.

Thank you Peter for your patience on this!

-- Rachel
http://google.com/profiles/Gnarlodious

0 new messages