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


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

Question about imports and packages

Started byGerald Britton <gerald.britton@gmail.com>
First post2016-05-24 19:35 -0400
Last post2016-05-24 23:36 -0400
Articles 3 — 3 participants

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 19:35 -0400
    Re: Question about imports and packages Steven D'Aprano <steve@pearwood.info> - 2016-05-25 11:02 +1000
      Re: Question about imports and packages Terry Reedy <tjreedy@udel.edu> - 2016-05-24 23:36 -0400

#109092 — Question about imports and packages

FromGerald Britton <gerald.britton@gmail.com>
Date2016-05-24 19:35 -0400
SubjectQuestion about imports and packages
Message-ID<mailman.70.1464132967.20402.python-list@python.org>
I'm trying to understand packages in Python, especially Intra Package
References.

>From https://docs.python.org/2/tutorial/modules.html#packages i see that:

you can use absolute imports to refer to submodules of siblings packages.



This is what I can't get to work in my case. Here's the setup:

directory testpkg containing three files:
1. an empty __init__.py

2. a testimport.py which has:


from testpkg.testimported import A
a = A()
print type(a)

3. a testimported.py which has:


class A():
    pass

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

However, I thought I was doing what the doc describes for intra package
imports. What am I missing?

Or is the problem simply that I do not have subpackages?

-- 
Gerald Britton

[toc] | [next] | [standalone]


#109097

FromSteven D'Aprano <steve@pearwood.info>
Date2016-05-25 11:02 +1000
Message-ID<5744f9b1$0$1603$c3e8da3$5496439d@news.astraweb.com>
In reply to#109092
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

[toc] | [prev] | [next] | [standalone]


#109099

FromTerry Reedy <tjreedy@udel.edu>
Date2016-05-24 23:36 -0400
Message-ID<mailman.76.1464147419.20402.python-list@python.org>
In reply to#109097
On 5/24/2016 9:02 PM, 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:

I have a similar setup, except with multiple files importing from 
imported file.  One way to make absolute imports within a package work, 
and how I do it, is to put the directory containing testpkg in a .pth 
file in the site-modules directory.  In particular, I have python.pth 
containing "F:/Python".  This effectively makes "Python" an extension of 
'site-packages', so when site-packages is searched for modules, so is 
Python.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


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


csiph-web