From: Richard Lamboj on 7 May 2010 06:55 Hello, I have a question about importing python modules. I have modul package, with submodules. So how can a submodul access a modul that is on level upper? Is there something like "import ../../blah"? I don't mean something like this: "import bla.blub.moep" Kind Regards, Richi
From: Alex Hall on 7 May 2010 07:02 I have a main folder. Inside that I have a "modes" package (subfolder holding __init__.py) as well as a "misc" package. When modes has to import helpers.py from misc, I use this: from .misc import helpers The period makes Python look up one level for misc, then go into it to find helpers. On 5/7/10, Richard Lamboj <richard.lamboj(a)bilcom.at> wrote: > > Hello, > > I have a question about importing python modules. > > I have modul package, with submodules. So how can a submodul access a modul > that is on level upper? > > Is there something like "import ../../blah"? I don't mean something like > this: "import bla.blub.moep" > > Kind Regards, > > Richi > -- > http://mail.python.org/mailman/listinfo/python-list > -- Have a great day, Alex (msg sent from GMail website) mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
From: Andi Albrecht on 7 May 2010 07:19 Richard Lamboj <richard.lamboj(a)bilcom.at> schrieb: > > Hello, > > I have a question about importing python modules. > > I have modul package, with submodules. So how can a submodul access a modul > that is on level upper? > > Is there something like "import ../../blah"? I don't mean something like > this: "import bla.blub.moep" > > Kind Regards, > > Richi Hi Richi, starting with 2.5 Python allows relative imports, e.g. import ..blah Andi
From: Jean-Michel Pichavant on 7 May 2010 07:50 Richard Lamboj wrote: > Hello, > > I have a question about importing python modules. > > I have modul package, with submodules. So how can a submodul access a modul > that is on level upper? > > Is there something like "import ../../blah"? I don't mean something like > this: "import bla.blub.moep" > > Kind Regards, > > Richi > I would advise to use absolute imports whenever possible (always ?). If 'pkg' is your package, then module sub1 should import sub2 that way: file pkg/sub1.py: import pkg.sub2 see http://www.python.org/dev/peps/pep-0328/#rationale-for-relative-imports " the python-dev community chose absolute imports as the default because they're the more common use case and because absolute imports can provide all the functionality of relative (intra-package) imports -- albeit at the cost of difficulty when renaming package pieces higher up in the hierarchy or when moving one package inside another." You can still use relative import if you want, they've been implemented for a purpose, but I woudl highly discourage the ambiguous relative imports of python 2.4. JM
From: Richard Lamboj on 7 May 2010 08:25 Am Friday 07 May 2010 13:50:15 schrieb Jean-Michel Pichavant: > Richard Lamboj wrote: > > Hello, > > > > I have a question about importing python modules. > > > > I have modul package, with submodules. So how can a submodul access a > > modul that is on level upper? > > > > Is there something like "import ../../blah"? I don't mean something like > > this: "import bla.blub.moep" > > > > Kind Regards, > > > > Richi > > I would advise to use absolute imports whenever possible (always ?). > If 'pkg' is your package, then module sub1 should import sub2 that way: > > file pkg/sub1.py: > > import pkg.sub2 > > > see http://www.python.org/dev/peps/pep-0328/#rationale-for-relative-imports > > " the python-dev community chose absolute imports as the default because > they're the more common use case and because absolute imports can > provide all the functionality of relative (intra-package) imports -- > albeit at the cost of difficulty when renaming package pieces higher up > in the hierarchy or when moving one package inside another." > > You can still use relative import if you want, they've been implemented > for a purpose, but I woudl highly discourage the ambiguous relative > imports of python 2.4. > > JM Hello, I always use absolute imports, but sometimes i does not work. I got: ImportError: No module named mail. When i import it from top of the module it works. If i import it from a level down, it works not always - strange? Kind Regards, Richi
|
Next
|
Last
Pages: 1 2 Prev: ActivePython - how to configure portable framework? Next: listing exported functions in a DLL |