Question about imports and packages

Gerald Britton gerald.britton at gmail.com
Tue May 24 19:35:58 EDT 2016


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



More information about the Python-list mailing list