I’m working on a package, and I have a structure like:
mypackage/
__init__.py
__main__.py
someotherstuff.py
test/
__init__.py
testsomeotherstuff.py
I’ve set it up so that the main.py function runs some unit tests, and executing python mypackage from the command-line works fine. However often I want to debut using ipython, but from the interpreter, run mypackage gives the error ERROR: File 'mypackage.py' not found. I can run it manually by doing run mypackage/__main__.py but somehow this seems wrong. Is there something else I should have done to set this up correctly?
Running a package as a program was introduced in Python 2.5. I don’t think IPython has a native feature for this, but starting with version 2.7, the Python standard library has, namely the
runpy.run_module()function. Note that this behaves slightly different than IPython’s%run, since it will return the global dictionary of the module instead of directly importing it into the interpreter scope.