Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running module from ipython interpreter instead of command line

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?

like image 799
tdc Avatar asked Mar 09 '12 15:03

tdc


People also ask

How do I run IPython interpreter?

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).

How do I exit IPython in CMD?

You can also hit Ctrl-D to exit out of ipython. In fact, using your keyboard is the most highly recommended way.

How do I run a Python program in IPython?

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.


1 Answers

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.

like image 187
Sven Marnach Avatar answered Nov 09 '22 06:11

Sven Marnach