Removing the stale bytecode file makes things work as expected: If you’re not a core developer on a Python implementation, the problem of importing stale bytecode is most likely to arise when renaming Python source files.
For Python implementation developers, it can also arise any time we’re working on the compiler components that are responsible for generating the bytecode in the first place - that’s the main reason the CPython The above are the common traps, but there are others, especially if you start getting into the business of extending or overriding the default import system.
Any absolute imports that expect “example” to be a top level package will still break though).
If I need to work in a different directory for some reason, well, that’s why I also like to have multiple shell sessions open.
While I’m using an embedded test case as an example here, similar issues arise any time you execute a script directly from inside a package without using the outside the package directory).
The procedure for setting environment variables in depends on what your default shell is.
BASH seems to be the most common, but CSH is also common.
This is just one more reason why main modules in more complex applications should be kept fairly minimal - it’s generally far more robust to move most of the functionality to a function or object in a separate module, and just import that module from the main module.