Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Makoto Kuwata Newsgroups: comp.lang.python Subject: [Q] ImportError by __import__() on Python >= 3.4 Date: Thu, 2 Jun 2016 23:04:50 +0900 Lines: 50 Sender: kwatch@gmail.com Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de ndb5a5BcuB+vec2j1PSLiQnIzDDESdZZbasCGMdnFyTw== 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: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; '3.3,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sender:addr:gmail.com': 0.18; 'try:': 0.18; 'thanks.': 0.18; 'subject:] ': 0.19; 'to:name:python-list@python.org': 0.20; 'os,': 0.22; 'errors': 0.23; 'import': 0.24; '(most': 0.24; 'module': 0.25; 'skip:" 20': 0.26; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; '13,': 0.29; 'mod': 0.29; 'raise': 0.29; 'code': 0.30; 'skip:& 30': 0.30; 'skip:_ 10': 0.32; 'traceback': 0.33; 'file': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'trouble': 0.35; 'done': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'skip:& 10': 0.37; 'received:209': 0.38; 'received:209.85.220': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'subject:[': 0.39; 'test': 0.39; 'to:addr:python.org': 0.40; 'export': 0.63; 'sample': 0.63; 'skip:\xc2 10': 0.67; '8bit%:100': 0.70; "'foo'": 0.84; 'makoto': 0.84; 'why?': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to; bh=RR4tG+rtYlgxWOTJEuiyHvqVilC4g7qyBzREOgjv55w=; b=jHWHicFmv379bFPjV7LJ4QNzWooGhZWxupHwNiDGsCzoZS9Vyclsrp66TZ1pA2N1CE fQSZ4nnTbtqENUIwwBwbkyzj8dMCHqoG3hPRMqkIjl1XQmV6JO5I6rnxA8bVVQyIqVTn mwbQWQZ3vsKCuw76ZreM4VH5lN1iMm9B1ATuDqk6XU9AQT5q9FnV63W6OAZxM2Db6JIZ j2xYekZ0yURdQknUDf77r5fY/GczZ7HBQJKD6C6lx2dlFmiIAEn8sZztzI1Rc7Jp6EmI NRm1iRsE5eq92ZGOUlZFczOXZ2X7ALNgJlWcS+yGL63uWLTsduBYoDW9zi1NTSRvwx3j jQcQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kuwata-lab-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:date:message-id:subject:from:to; bh=RR4tG+rtYlgxWOTJEuiyHvqVilC4g7qyBzREOgjv55w=; b=fQXa9R8cRerT+EbVVDBoAnyMuEXo8zVLWgh91S2WDrrD8YLtEQQ/bKAlwsAu8WJMw1 KksLQ/U3RDj2FM/u6zMCMn1Kpq2h8tjCUB5eXdQ2oxE7GkXlB4EJNM+oogjC5pUq04OQ ptfkLDjp7t/iq4mtKIgFZB/rF32prZsAKU8k0v18aVV/uEEdcQgKZ/Io7V6htNKwdOvY 0U124plXbVjJYLgMcDcG1NQk6AkfyT/pJd02JiHWMGH3fMmaxdEbTg060NjP/nU24bNP 7Npzx2X7BDBfnrWsjq9kLnsBqZzooCHYE1sL5L2Ho/G5e/+NrtN/55qJc6tMhJ/GheRD QCHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:date:message-id:subject:from :to; bh=RR4tG+rtYlgxWOTJEuiyHvqVilC4g7qyBzREOgjv55w=; b=Ij/MSRxwSv+p83Qwb3DWHlpI2/4sQo5iIe/+YIXiY3KiHRAYgcP6d3rjMdZVNiHY0S X+xVyCRT+WugKBrqz7S6UPJrUuIqVm7SUtL1OakFFwOJy7qFbBMdGmCpfRkXQxCbyAMV L5W9OBrCjbBvtKEtyJbuCaKElrUQsccBuX6sLTexsiFTfKg6SHRET52XsEo6O7IEuBR5 nz+AZ4ZFApruV7yocua0JBXHAzwAER0LfiQMitPMHkATtpFHYjizasEjLD1YDh56DFjB zVbXSGeTFTSDof2dykO7KLChLYkji6rSqWHX+8IXuLQNsXLeKO284XNzTP754PLDZhqb rnIA== X-Gm-Message-State: ALyK8tLUtVxvyoMpd6C7q1OLYJLabrka7jmSZGDx9wsuPNUe/YNkNvkFrKAUhLKJ6ksP1oj8crtUHI5WVOGa8g== X-Received: by 10.55.186.132 with SMTP id k126mr42312889qkf.176.1464876290613; Thu, 02 Jun 2016 07:04:50 -0700 (PDT) X-Google-Sender-Auth: iA4XPbR6M8NCiZAbyz5gP-T5Q7g 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:109367 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. -- regards, makoto