Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109098 > unrolled thread

Question about imports and packages

Started byGerald Britton <gerald.britton@gmail.com>
First post2016-05-24 22:35 -0400
Last post2016-05-24 22:35 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Question about imports and packages Gerald Britton <gerald.britton@gmail.com> - 2016-05-24 22:35 -0400

#109098 — Question about imports and packages

FromGerald Britton <gerald.britton@gmail.com>
Date2016-05-24 22:35 -0400
SubjectQuestion about imports and packages
Message-ID<mailman.75.1464143724.20402.python-list@python.org>
On Wed, 25 May 2016 10:00 am, Steven D'Aprano wrote:
>On Wed, 25 May 2016 09:35 am, Gerald Britton wrote:
>
>For brevity, here's your package setup:
>
>
>testpkg/
>+-- __init__.py
>+-- testimport.py which runs "from testpkg.testimported import A"
>+-- testimported.py containing class A
>
>Your package layout is correct. But:
>
>> When I run
>>
>> python testimport.py
>>
>> I get:
>>
>> Traceback (most recent call last):
>> File "testimport.py", line 1, in <module>
>> from testpkg.testimported import A
>> ImportError: No module named testpkg.testimported
>
>The problem is that you are running the Python script from *inside* the
>package. That means, as far as the script can see, there is no longer a
>package visible -- it cannot see its own outside from the inside.
>
>cd to a directory *outside* the package, and run:
>
>python testpkg/testimport.py
>
>and it should work. Better: make sure the testpkg directory is somewhere in
>your PYTHONPATH, and run:
>
>python -m testpkg.testimport
>
>
>which tells Python to search for the package, rather than using a hard-coded
>path.
>
>
>The package directory has to be visible to the import system. That means it
>must be INSIDE one of the locations Python searches for modules. The
>current directory will do, but to be absolutely clear, that means that the
>package directory itself must be IN the current directory:
>
>
>./
>+-- pkgtest/
>    +-- __init__.py
>    +-- testimport.py
>    +-- testimported.py
>
>
>Or you can use any other directory found in sys.path.
>
>
>
>--
>Steven

Thanks for the explanation, Steven.  I'm just having trouble
reconciling this with the docs which seems to imply that an intra
package import should work from inside the package.

This bit:

"When packages are structured into subpackages (as with the sound
package in the example), you can use absolute imports to refer to
submodules of siblings packages. For example, if the module
sound.filters.vocoder needs to use the echo module in the
sound.effects package, it can use from sound.effects import echo."

seems to say that what I was trying to do should work, since I'm
trying to use an absolute import to refer to a sibling module.  From
that I got the idea that the importer will ascend the package tree as
well as follow it down.

Or is the key idea here *submodule*? Since there are no modules in the
"sound" package in the example, only other packages?

FWIW this all came about when I was using pylint on a package where
the imports were relative.  Pylint tells me to add the package name as
the first qualifier (it warns about relative imports), which I tried
to do, only to find that doesn't work.

-- 
Gerald Britton

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web