Ejecutando el módulo desde el intérprete de ipython en lugar de la línea de comandos

I'm working on a package, and I have a structure like:


I've set it up so that the principal.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 da el 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?

preguntado el 09 de marzo de 12 a las 15:03

The ability to do %run -m mypackage was added in a recent version - I don't know if that works for your case. -

1 Respuestas

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.

respondido 09 mar '12, 15:03

Eso me da un error: ImportError: mypackage is a package and cannot be directly executed - TDC

@tdc: What Python version are you using? - Sven Marnach

Python 2.6.5 - which makes it odd that it runs from the command line then? - TDC

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.