Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: [Q] ImportError by __import__() on Python >= 3.4 Date: Fri, 3 Jun 2016 01:31:02 +0100 Lines: 61 Message-ID: References: <07867e05-0763-5a2e-b3b1-906bcf3fb1f0@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de UBKnItZZtVfIRoCfX8bJNg5bz42jeWKm0OyMaP1ckNEA== 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; 'subject:: [': 0.03; 'subject:Python': 0.05; '(python': 0.05; 'finally:': 0.05; 'importerror:': 0.05; 'raises': 0.07; 'shutil': 0.07; 'skip:/ 10': 0.07; 'sys,': 0.07; '###': 0.09; "'w')": 0.09; 'importerror': 0.09; 'example:': 0.10; 'python': 0.10; 'output': 0.13; 'def': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iteration': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'thanks.': 0.18; 'subject:] ': 0.19; 'os,': 0.22; 'errors': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; 'script': 0.25; 'testing': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'error': 0.27; "skip:' 10": 0.28; '13,': 0.29; 'mod': 0.29; 'raise': 0.29; 'code': 0.30; 'skip:_ 10': 0.32; 'traceback': 0.33; 'file': 0.34; 'trouble': 0.35; 'done': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'created': 0.36; 'to:addr:python-list': 0.36; 'really': 0.37; 'things': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'test': 0.39; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'export': 0.63; 'sample': 0.63; "'foo'": 0.84; 'kuwata': 0.84; 'makoto': 0.84; 'why?': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=aYR_tW9IEGvEQ1O_HhQA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 In-Reply-To: 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: <07867e05-0763-5a2e-b3b1-906bcf3fb1f0@mrabarnett.plus.com> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109391 On 2016-06-02 15:04, Makoto Kuwata wrote: > Hi, > > I have a trouble around __import__(). > The following sample code works well on Python <= 3.3, > but it raises ImportError on Python >= 3.4. > > > ## importtest.py > import sys, os, shutil > > def test(name): > try: > ## create 'foo/__init__.py' file > os.mkdir(name) > with open(name + "/__init__.py", 'w') as f: > f.write("X=1") > f.flush() > ## ipmort 'foo' module > mod = __import__(name) > finally: > if os.path.isdir(name): > shutil.rmtree(name) > > test("foo") # no errors > test("bar") # may raise error on Python >= 3.4. Why? > > > Output Example: > > ### No errors (Python <= 3.3) > ubuntu$ export PYTHONPATH=. > ubuntu$ for x in 1 2 3 ; do /usr/bin/python3.3 importtest.py; done > > ### ImportError (Python >= 3.4) > ubuntu$ export PYTHONPATH=. > ubuntu$ for x in 1 2 3 ; do /usr/bin/python3.4 importtest.py; done > Traceback (most recent call last): > File "tmp/importtest.py", line 19, in > test("bar") # may raise error on Python >= 3.4. Why? > File "tmp/importtest.py", line 13, in test > mod = __import__(name) > ImportError: No module named 'bar' > > > Please give me any advices or hints. > Thanks. > Things to try: Does the order matter? If you try "bar" then "foo" does "foo" fail? Does the directory matter? Is there something called "bar" in the directory already? What does the created "bar" directory contain? Does it really contain only "__init__.py"? You're testing the script 3 times; on which iteration does it fail?