Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Py3: Import relative path module

Reply
Thread Tools

Py3: Import relative path module

 
 
Gnarlodious
Guest
Posts: n/a
 
      10-31-2010
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
 
Reply With Quote
 
 
 
 
Дамјан Георгиевски
Guest
Posts: n/a
 
      10-31-2010


> 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


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!

 
Reply With Quote
 
 
 
 
Gnarlodious
Guest
Posts: n/a
 
      11-01-2010
On Oct 31, 11:09*am, Дамјан Георгиевски wrote:
> from .. import Data.DumpHT as DumpHT


That doesn't work. Any more ideas?

-- Gnarlie
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-01-2010
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". 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
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      11-01-2010
On Nov 1, 2:16*am, Steven D'Aprano <steve-REMOVE-
(E-Mail Removed)> 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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      11-01-2010
Gnarlodious wrote:

> On Nov 1, 2:16 am, Steven D'Aprano <steve-REMOVE-
> (E-Mail Removed)> 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?


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
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      11-01-2010
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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      11-01-2010
Gnarlodious wrote:

> 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.


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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      11-01-2010
On 11/1/2010 6:54 AM, Gnarlodious wrote:
> On Nov 1, 2:16 am, Steven D'Aprano<steve-REMOVE-
> (E-Mail Removed)> 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?


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


 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      11-03-2010
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
 
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
imp.find_module don't found my module but standard import statement can import this module why ? Stphane Klein Python 0 07-05-2011 07:36 AM
Why 'import module' will not import module.py but the directorymodule? Peng Yu Python 0 10-31-2009 11:51 PM
xsl:import with relative path and jar packaging problem Symon Java 1 05-15-2006 04:58 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:08 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:01 PM



Advertisments