3 Python shell for SymPy. Executes the commands in the "init_code" variable
4 before giving you a shell.
7 -c : permits to specify a python interactive interpreter, currently only
8 python or ipython. Example usage:
10 default is set to ipython
12 -h : prints this help message
14 --version: prints version number
18 from __future__ import division
20 x, y, z = symbols('xyz')
21 k, m, n = symbols('kmn', integer=True)
22 Basic.set_repr_level(2) # 2D output
23 pprint_try_use_unicode() # try to setup unicode pprint
28 # hook in-tree sympy into python path if running in-tree version of isympy
30 isympy_dir
= os
.path
.dirname(__file__
) # bin/isympy.py
31 sympy_top
= os
.path
.split(isympy_dir
)[0] # ../
32 sympy_dir
= os
.path
.join(sympy_top
, 'sympy') # ../sympy/
33 if os
.path
.isdir(sympy_dir
):
34 #print 'working in-tree...'
35 sys
.path
.insert(0, sympy_top
)
37 from sympy
import __version__
as sympy_version
38 python_version
= "%d.%d.%d" % tuple(sys
.version_info
[:3])
41 """Puts ">>> " in front of each line."""
43 for l
in s
.split("\n"):
49 "Python %s console for SymPy %s. These commands were executed:\n%s" % \
50 (python_version
, sympy_version
, indent(init_code
))
52 def run_ipython_interpreter():
54 from IPython
.Shell
import IPShellEmbed
56 #is the -nopprint necessary?
59 ipshell
= IPShellEmbed(args
)
60 api
= ipshell
.IP
.getapi()
62 api
.ex('__IP.compile("from __future__ import division", "<input>", "single") in __IP.user_ns')
64 ### create some magic commands
66 #def pretty_print(self, arg):
67 # self.api.ex("print str((%s).__pretty__())" % arg)
69 #api.expose_magic("pprint", pretty_print)
71 # Now start an embedded ipython.
73 sys
.exit("Exiting ...")
75 def run_python_interpreter():
77 Couldn't locate IPython. Having IPython installed is greatly recommended. See
78 http://ipython.scipy.org for more details. If you use Debian/Ubuntu, just
79 install the "ipython" package and start isympy again.\n"""
87 class HistoryConsole(code
.InteractiveConsole
):
88 def __init__(self
, locals=None, filename
="<console>",
89 histfile
=os
.path
.expanduser("~/.sympy-history")):
90 code
.InteractiveConsole
.__init
__(self
)
91 self
.init_history(histfile
)
92 self
.runcode(self
.compile("from __future__ import division",
95 def init_history(self
, histfile
):
96 readline
.parse_and_bind("tab: complete")
97 if hasattr(readline
, "read_history_file"):
99 readline
.read_history_file(histfile
)
102 atexit
.register(self
.save_history
, histfile
)
104 def save_history(self
, histfile
):
105 readline
.write_history_file(histfile
)
107 sh
= HistoryConsole()
108 sh
.runcode(init_code
)
109 sh
.interact(welcome_msg
)
110 sys
.exit("Exiting ...")
113 from optparse
import OptionParser
116 from sympy
import __version__
as sympy_version
117 parser
= OptionParser("usage: isympy [options]", version
=sympy_version
)
118 parser
.add_option("-c", "--console", dest
="console",
119 help="specify a python interactive interpreter, python or ipython")
120 (options
, args
) = parser
.parse_args()
122 if options
.console
== "python":
123 run_python_interpreter()
124 elif options
.console
== "ipython":
125 run_ipython_interpreter()
128 run_ipython_interpreter()
131 run_python_interpreter()
133 if __name__
== "__main__":