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?

1
  • 6
    do you still have .pyc files? Commented May 24, 2013 at 16:59

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

What? How can I turn this off?
Just edited the question with more info and links to lead you on. Hope they'll be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.