Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Gerald Britton Newsgroups: comp.lang.python Subject: Question about imports and packages Date: Tue, 24 May 2016 22:35:21 -0400 Lines: 89 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de aHz1vKIe1zKCUEyiZ80JEgKb4bnmGmaB8KhWR/hGajfw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importerror:': 0.05; 'subject:Question': 0.05; 'correct.': 0.07; 'imply': 0.07; 'means,': 0.07; 'imports': 0.09; 'modules.': 0.09; 'python': 0.10; 'package,': 0.13; 'do,': 0.15; 'wed,': 0.15; 'packages.': 0.15; '+--': 0.16; '10:00': 0.16; '2016': 0.16; '>on': 0.16; '__init__.py': 0.16; 'better:': 0.16; 'echo': 0.16; 'fwiw': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run:': 0.16; 'wrote:': 0.16; 'tells': 0.18; 'tree': 0.18; '>': 0.18; 'runs': 0.18; 'work,': 0.21; 'visible': 0.22; 'trying': 0.22; 'am,': 0.23; 'seems': 0.23; 'absolute': 0.23; 'tried': 0.24; 'import': 0.24; '(most': 0.24; 'somewhere': 0.24; 'module': 0.25; 'script': 0.25; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'locations': 0.27; 'module.': 0.27; 'package.': 0.27; 'see,': 0.27; 'idea': 0.28; '(it': 0.29; 'searches': 0.29; "i'm": 0.30; 'work.': 0.30; 'relative': 0.30; 'run': 0.33; 'class': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'running': 0.34; 'add': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'trouble': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'skip:& 10': 0.37; 'say': 0.37; 'thanks': 0.37; 'itself': 0.38; 'received:209': 0.38; 'means': 0.39; 'sure': 0.39; 'received:209.85.214': 0.39; 'system.': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'here': 0.66; 'sound': 0.72; 'directory:': 0.84; 'sibling': 0.84; 'absolutely': 0.88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=OvPBNI+/lqVDitgdcNY1LCaCdhYw0wdU+nsOk4klvO8=; b=ZTBDYJ8ncN9lnZGRTUO8EoqN3rVjM4LAKYvtVubFCI+EKKm5quXSJ+mzhfZ3/aWWzf woIEEEJJNZExvhgQRghh6LthuE/snLlcxZZQzc79+7pu/idP3AnS2k5mRlmIs2Cg+3LL 3ok21ZlnSBiYsFsrMnwS0GB8IXakGro/26sXploJcOOpMnF5EARBeOFDJW5tTXAlbd4b BEiawZgXwcD/XgoiJ7UHBBmoJCBwtLtp46zF0rpRGCa4p/TmjvvUgiC0w+wjo8MoZlo/ i4xtoj/2NxiFV6HS/JdiFFZjTIa02bRMqHThsCJYAAckQH1FiklX7Lu6ZgnaihQYG7oU u9Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=OvPBNI+/lqVDitgdcNY1LCaCdhYw0wdU+nsOk4klvO8=; b=POfOwFvwe4Y5mAmTkjj91lZGv+5EwZWTdrUzkifGQAJt7yFD5ll0+D79Ig0xHb/AyS 4gs8KW8mc0QJ/Hql2vofVx6hzNeVj1J9opcqiGpUSBhzZxJc1dMGd7PTlH3aj5AOca5b chVCcmb+Chgllh4OCXKCPziSY9RNfACgccIfusPsNcTLhNl/57oL8WoPjnth9P8J4wuE tr8zrU1fBkaYJhLb7cNyeDNUUHohKSzz/byHODQ8uVKqLsYgrSeT/hMYZ+1uAAXLfcz2 wyet/YktvD8jMP48yuRSWtwGnGwQioAsGty/JpSuNW66itZrGc0QGwDGfd1ogHzNdb1Z 26DQ== X-Gm-Message-State: AOPr4FW90H7kPk8hqc7ScoDPywGyIQekyD9ZTtVvyDBEk8smV45SRGf94v4337gPwGOzl7wmf5ZKjtoEgEUumg== X-Received: by 10.36.78.20 with SMTP id r20mr21155001ita.20.1464143721380; Tue, 24 May 2016 19:35:21 -0700 (PDT) X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com comp.lang.python:109098 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 >> 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