3 """Python shell for SymPy.
5 This is just a normal Python shell (ipython shell if you have the
6 IPython package installed) that executes the following commands so
7 that you don't have to:
9 >>> from __future__ import division
10 >>> from sympy import *
11 >>> x, y, z = symbols("xyz")
12 >>> k, m, n = symbols("kmn", integer=True)
15 So starting 'isympy' is equivalent to starting Python (or IPython)
16 and executing the above commands by hand. It is intended for easy
17 and quick experimentation with SymPy.
22 -c CONSOLE, --console=CONSOLE
24 Use the specified python or ipython shell (lower case) as console
25 backend instead of the default one (ipython if present or python
26 otherwise), eg. isympy -c python
30 Print only Python's and SymPy's versions to stdout at startup.
34 Additionally you can pass command line options directly to IPython
35 interpreter (standard Python shell is not supported). However you
36 need to add '--' separator between two types of options, eg. to run
37 SymPy without startup banner and colors, issue:
39 isympy -q -- -colors NoColor
45 # common SymPy's code for IPython and Python shells
47 div_code
= "from __future__ import division"
48 sym_code
= "from sympy.interactive import *"
50 # hook in-tree SymPy into Python path, if possible
52 isympy_dir
= os
.path
.dirname(__file__
) # bin/isympy
53 sympy_top
= os
.path
.split(isympy_dir
)[0] # ../
54 sympy_dir
= os
.path
.join(sympy_top
, 'sympy') # ../sympy/
56 if os
.path
.isdir(sympy_dir
):
57 sys
.path
.insert(0, sympy_top
)
59 # some longer messages
62 These commands were executed:
63 >>> from __future__ import division
64 >>> from sympy import *
65 >>> x, y, z = symbols('xyz')
66 >>> k, m, n = symbols('kmn', integer=True)
69 Documentation can be found at http://sympy.org/
73 Couldn't locate IPython. Having IPython installed is greatly recommended.
74 See http://ipython.scipy.org for more details. If you use Debian/Ubuntu,
75 just install the 'ipython' package and start isympy again.
78 def init_ipython(*args
, **kwargs
):
81 ip
= IPython
.Shell
.make_IPython(kwargs
.get('argv'))
83 from sympy
import pretty
85 def result_display(self
, arg
):
86 """Pretty-printer display hook.
88 Called for displaying pretty results to the user. Using this
89 handler not only SymPy's expression can be printed but also
90 Python's lists, tuples and dictionaries.
92 This function was adapted from:
94 ipython/IPython/hooks.py:155
107 ip
.set_hook('result_display', result_display
)
109 ip
.runcode(ip
.compile(div_code
, '<input>', 'single'))
110 ip
.runcode(ip
.compile(sym_code
, '<input>', 'single'))
112 ip
.interact(kwargs
.get('message'))
113 sys
.exit('Exiting ...')
115 def init_python(*args
, **kwargs
):
118 class HistoryConsole(code
.InteractiveConsole
):
119 def __init__(self
, *args
, **kwargs
):
120 code
.InteractiveConsole
.__init
__(self
)
122 history
= kwargs
.get('history', '~/.sympy-history')
123 history
= os
.path
.expanduser(history
)
126 import readline
, atexit
128 readline
.parse_and_bind('tab: complete')
130 if hasattr(readline
, 'read_history_file'):
132 readline
.read_history_file(history
)
136 def save_history(history
):
137 readline
.write_history_file(history
)
139 atexit
.register(save_history
, history
)
143 from sympy
import pprint
144 sys
.displayhook
= pprint
146 console
= HistoryConsole(*args
, **kwargs
)
148 console
.runcode(console
.compile(div_code
, '<input>', 'single'))
149 console
.runcode(console
.compile(sym_code
, '<input>', 'single'))
151 console
.interact(kwargs
.get('message'))
152 sys
.exit('Exiting ...')
155 from sympy
import __version__
as sympy_version
156 py_version
= "%d.%d.%d" % sys
.version_info
[:3]
158 usage
= 'usage: isympy [options] -- [ipython options]'
160 key_options
= ('dest', 'action', 'default', 'help')
164 ('-c', '--console', 'console', 'store', None,
165 'select an interactive console: python | ipython'),
167 ('-q', '--quiet', 'quiet', 'store_true', False,
168 'print only version information at startup'),
171 from optparse
import OptionParser
173 parser
= OptionParser(usage
, version
=sympy_version
)
175 for option
in isympy_options
:
176 parser
.add_option(*option
[0:2],
177 **dict(zip(key_options
, option
[2:])))
179 (options
, args
) = parser
.parse_args()
181 message
= "Python %s console for SymPy %s" \
182 % (py_version
, sympy_version
)
184 if os
.getenv('SYMPY_USE_CACHE') == 'no':
185 message
+= ' (cache: off)\n'
189 if not options
.quiet
:
190 message
+= '\n' + long_message
198 'ipython' : init_ipython
,
199 'python' : init_python
203 shells
[options
.console
](**args
)
211 if __name__
== "__main__":