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
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!
That doesn't work. Any more ideas?
-- Gnarlie
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
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
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
> 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
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
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
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
Or, to put it another way, if __name__ is "__main__" then it won't work.
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.
> 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
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
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
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
> |-- 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
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!