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


Groups > comp.lang.python > #109436

Re: [Q] ImportError by __import__() on Python >= 3.4

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: [Q] ImportError by __import__() on Python >= 3.4
Date Fri, 3 Jun 2016 19:15:52 +0100
Lines 90
Message-ID <mailman.134.1464977766.1839.python-list@python.org> (permalink)
References <CAFTm5Rv+K6KFMt2FXfWfZtn5gAnJtf4XawfijRpO4vcJf73h7g@mail.gmail.com> <07867e05-0763-5a2e-b3b1-906bcf3fb1f0@mrabarnett.plus.com> <CAFTm5Rs4kDaj1y-pbu7gqtkUA=bNKbCuhU_YRP4gb0LyukWxZg@mail.gmail.com> <60d42dc5-f966-1baf-f044-bb7c07f58172@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 xDAtfvAjpIBj1nf3UgaQFQu3AJ/Sni+B9RHZF/bv7Bhg==
Return-Path <python@mrabarnett.plus.com>
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; 'mrab': 0.05; 'removes': 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; "hasn't": 0.15; '2016': 0.16; '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; 'to:name:python-list@python.org': 0.20; 'os,': 0.22; 'file.': 0.22; 'am,': 0.23; 'errors': 0.23; 'finished': 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; 'fri,': 0.27; 'module.': 0.27; "skip:' 10": 0.28; '13,': 0.29; 'mod': 0.29; 'raise': 0.29; 'code': 0.30; 'e.g.': 0.30; 'skip:_ 10': 0.32; 'traceback': 0.33; 'file': 0.34; 'trouble': 0.35; 'done': 0.35; 'fail': 0.35; 'something': 0.35; 'sometimes': 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; 'some': 0.40; 'no.': 0.62; 'export': 0.63; 'sample': 0.63; 'detail.': 0.66; 'matter.': 0.66; 'race': 0.72; 'sounds': 0.76; "'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=EBOSESyhAAAA:8 a=LS4zBt27BPNO9JMbm84A:9 a=QEXdDO2ut3YA:10 a=yJM6EZoI5SlJf8ks9Ge_:22
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 <CAFTm5Rs4kDaj1y-pbu7gqtkUA=bNKbCuhU_YRP4gb0LyukWxZg@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <60d42dc5-f966-1baf-f044-bb7c07f58172@mrabarnett.plus.com>
X-Mailman-Original-References <CAFTm5Rv+K6KFMt2FXfWfZtn5gAnJtf4XawfijRpO4vcJf73h7g@mail.gmail.com> <07867e05-0763-5a2e-b3b1-906bcf3fb1f0@mrabarnett.plus.com> <CAFTm5Rs4kDaj1y-pbu7gqtkUA=bNKbCuhU_YRP4gb0LyukWxZg@mail.gmail.com>
Xref csiph.com comp.lang.python:109436

Show key headers only | View raw


On 2016-06-03 06:48, Makoto Kuwata wrote:
> On Fri, Jun 3, 2016 at 9:31 AM, MRAB <python@mrabarnett.plus.com 
> <mailto:python@mrabarnett.plus.com>> wrote:
>
>     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 <module>
>                 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?
>
>
> Yes. Name is not matter. Order is matter.
>
>
>     Does the directory matter?
>
>
> No. I created "foo" and "bar" directories in order to create python 
> module.
>
>
>     Is there something called "bar" in the directory already?
>
>
> No. Sample script removes directories every time.
>
>
>     What does the created "bar" directory contain? Does it really
>     contain only "__init__.py"?
>
>
> Yes. See sample script for detail.
>
>     You're testing the script 3 times; on which iteration does it fail?
>
>
> Because sometimes script will fail and sometimes will work file.
>
It sounds like it's some kind of race condition, e.g. it hasn't finished 
writing the file before it does the import.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: [Q] ImportError by __import__() on Python >= 3.4 MRAB <python@mrabarnett.plus.com> - 2016-06-03 19:15 +0100

csiph-web