0

I create a.py and b/__init__.py:

    $ ls
    $ cat > a.py
    import b
    print "a"
    $ mkdir b
    $ cat > b/__init__.py
    print "b"

It works as expected:

    $ python a.py 
    b
    a

I remove b/__init__.py and create b.py in the top folder:

    $ rm b/__init__.py
    $ cat > b.py
    print "new b"
    $ python a.py 
    b
    a

It doesn't print "new b", instead it still prints "b". Why?

Dog
  • 7,707
  • 8
  • 40
  • 74

1 Answers1

4

You just removed b/__init__.py but there sure is a b/__init__.pyc and in your a.py you're still importing b (import b). This may be causing the interpreter to assume is already bytecompiled because it sees b/__init__.pyc and take this which makes it print the same order.

Note that this command rm b/__init__.py just removed the file, not the folder nor the .pyc. That might be the reason.

Try rm -r b and your code should work as expected.

This is indeed the expected behavior with the python interpreter. If you don't want the interpreter to byte-compile files you can call it with the -B parameter but this is not recommended for performance issues because byte-compiled code makes the program runs much faster.

You can read more about modules and byte-compiled files and search paths in the docs.

Hope this helps!

Community
  • 1
  • 1
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73