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?
Start by launching the IPython interpreter by typing ipython on the command-line; alternatively, if you've installed a distribution like Anaconda or EPD, there may be a launcher specific to your system (we'll discuss this more fully in Help and Documentation in IPython).
You can also hit Ctrl-D to exit out of ipython. In fact, using your keyboard is the most highly recommended way.
You can use run command in the input prompt to run a Python script. The run command is actually line magic command and should actually be written as %run. However, the %automagic mode is always on by default, so you can omit this.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With