I install ipython, and then i run
./paster shell dev.ini
command, paster open standard python console. How can I make it run ipython?
Here's how it worked for me on Fedora 17 with IPython 0.12, paste-1.7.5.1 and pylons 1.0:
$ paster shell dev.ini
Pylons Interactive Shell
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)]
All objects from project.lib.base are available
Additional Objects:
mapper - Routes mapper object
wsgiapp - This project's WSGI App instance
app - paste.fixture wrapped around wsgiapp
>>> __name__ = '__main__'
>>> import IPython
>>> IPython.embed()
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Resetting __name__ is necessary because either Pylons/Paste sets __name__ to "pylons-admin" which confuses IPython (it tries to lookup the main module by its name in sys.modules).
I solved this problem downgrading ipython to version 0.10
Did you try the steps from the Pylons Quick Site Development:
13.1.2 Using an IPython embedded shell
IPython provides a more powerfull interactive prompt and a powerful
embedded shell. If you are a Python programmer and have not yet tried
IPython, you definitely should look into it.
First, import from IPython -- Add something like the following at the
top of your controller module, in our case in
firstapp/controllers/firstcontroller.py:
from IPython.Shell import IPShellEmbed args = ['-pdb', '-pi1', 'In
<\#>: ', '-pi2', ' .\D.: ',
'-po', 'Out<\#>: ', '-nosep'] ipshell = IPShellEmbed(args,
banner = 'Entering IPython. Press Ctrl-D to exit.',
exit_msg = 'Leaving Interpreter, back to Pylons.')
Then, place this code in your action/method:
ipshell('We are at action abc')
Return to Pylons and continue on responding to the request by pressing
Ctrl-D.
Note that because of some idiosyncratic feature of
IPython.Shell.IPShellEmbed, I had to put the following before each
call to ipshell():
ipshell.IP.exit_now = False ipshell('We are at action abc')
Is there any chance that your ipython is installed globally, but that you're running pylons from a virtualenv that is --no-site-packages? If that's the case, then pylons won't see your installation of ipython.
Ipython updated to version .11 just recently. Some of the components no longer work correctly, especially with 3rd party libraries. Check the mailing lists for pylons, and you may want to consider a bug report.
Try this:
paster ishell dev.ini
Related
I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.
When debugging, I often drop into an IPython shell for interactive code testing. However this also causes a large info dump of the python version, date and help instructions to stdout. How can I suppress this information so it doesn't obscure the intentional debugging messages?
x = get_important_data()
print 'important info! right here! The data is in variable "x"'
import IPython
IPython.embed()
This code gives output like this...
important info! right here! The data is in variable "x"
Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec 6 2015, 18:57:58)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
You can do this:
IPython.embed(banner1="")
Setting banner1 to empty string would make the startup messages go away. It will not actually remove the messages but will replace them with empty strings.
You can also add useful messages using the banner1, banner2 and exit_msg parameters:
IPython.embed(
banner1="Entering Debug Mode",
banner2="Here's another helpful message",
exit_msg="Exiting the debug mode!"
)
If you ever need to launch IPython instance from the command line, you can do this:
ipython --no-banner
I'm trying to set a custom banner for the IPython qtconsole (v3.0.0). In my profile configuration, I set c.IPythonWidget.banner = u'Custom Banner', and then launch ipython qtconsole --profile=myprof. What I get is my custom banner pre-pended to the regular banner:
Custom BannerPython 2.7.5 (default, Mar 9 2014, 22:15:05)
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
IPython profile: myprof
In [1]:
How can I get ONLY my banner to print, e.g.
Custom Banner
IPython profile: myprof
In [1]:
Thanks.
It's not ideal, but I was able to suppress the default banner by setting default_gui_banner=""in the core/usage.py module in the IPython source. I was not able to find any way to do this without modifying the source (which is ugly), so if anyone has a better way, I'm all ears.
The message you don't want is printed as part of the kernel's banner (which it gets from its shell). I was able to avoid printing it by setting the shell's banner1 attribute to an empty string before using/connecting to the kernel elsewhere:
from IPython.qt.inprocess import QtInProcessKernelManager
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel_manager.kernel.shell.banner1 = ""
I've only worked with/tested this with the IPython Qt stuff, so this might not work for other kernels.
I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.
I've installed and configured PyDev version 1.6.5.2011020317 inside Eclipse, running on Mac OS X 10.6.6:
Version: Helios Service Release 1
Build id: 20100917-0705
I used 'Auto Config' to set up my Python interpreter: it correctly found /usr/bin/python (which is Python version 2.6.1) and added various system folders to the PYTHONPATH, including /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC. Now that path is the correct path to the Foundation module in OS X, as evinced by the command-line interpreter:
$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Foundation
>>> Foundation.__path__
['/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/Foundation']
So why does PyDev complain about "Undefined variable from import: NSDictionary" on this class:
import Foundation
class MyClass(object):
def __init__(self, projectPath):
'''
Constructor
'''
self.projectDict = Foundation.NSDictionary.dictionaryWithContentsOfFile_(projectPath)
when I can use that class without any problem from the command-line interpreter?
Update: OK, I found out why it complains, which is that the Foundation module is using ScriptingBridge to dynamically generate the classes - presumably pydev isn't actually importing the module to see what classes are inside, it's just looking for .py[c] files. So let my question not be "why does this happen", but "what do I do to fix it"?
Why does this happen?: PyDev has no support for parsing the PyObjC scripting bridge metadata, and therefore has no way of introspecting / extracting symbols for many of the PyObjC classes.
What to do to fix it: In the PyDev source code there are several Python scripts which handle discovery of this metadata. The scripts are executed by Eclipse using the configured interpreter, and they return strings which are used to configure the interpreter, fill in completion lists, show usage tips, etc.
The scripts which seem relevant to your needs are:
interpreterInfo.py - called to obtain the list of directories and other default top-level imports for a given interpreter.
importsTipper.py - generates usage tips for a given symbol.
pycompletion.py - generates a list of completions for a given symbol.
Examples of calls to the above scripts using the system interpreter:
% /usr/bin/python interpreterInfo.py | grep PyObjC
|/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjCINS_PATH
Generate completions for a module:
>>> import pycompletion
>>> print pycompletion.GetImports('os')
##COMPLETIONS(/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py,(EX_CANTCREAT, 3),(EX_CONFIG, 3),(EX_DATAERR, 3), ....
It seems possible to create a simple macobjc.py library with routines which detect and read the PyObjC.bridgesupport file(s). The PyDev scripts could be modified to call into this library in order to return the list of valid completions for those classes. You'll need to point Eclipse to a local copy of the PyDev source in order to develop and test your patches against these files. Once you're done you can send it upstream; I have to believe the PyDev folks would accept a well-written patch to support PyObjC completions.