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


Groups > comp.lang.python > #109367 > unrolled thread

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

Started byMakoto Kuwata <kwa@kuwata-lab.com>
First post2016-06-02 23:04 +0900
Last post2016-06-02 23:04 +0900
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [Q] ImportError by __import__() on Python >= 3.4 Makoto Kuwata <kwa@kuwata-lab.com> - 2016-06-02 23:04 +0900

#109367 — [Q] ImportError by __import__() on Python >= 3.4

FromMakoto Kuwata <kwa@kuwata-lab.com>
Date2016-06-02 23:04 +0900
Subject[Q] ImportError by __import__() on Python >= 3.4
Message-ID<mailman.94.1464876294.1839.python-list@python.org>
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.

--
regards,
makoto

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web