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)
23 Basic.set_repr_level(2) # pretty print output; Use "1" for python output
24 pprint_try_use_unicode() # use unicode pretty print when available
29 # hook in-tree sympy into python path if running in-tree version of isympy
31 isympy_dir
= os
.path
.dirname(__file__
) # bin/isympy
32 sympy_top
= os
.path
.split(isympy_dir
)[0] # ../
33 sympy_dir
= os
.path
.join(sympy_top
, 'sympy') # ../sympy/
34 if os
.path
.isdir(sympy_dir
):
35 #print 'working in-tree...'
36 sys
.path
.insert(0, sympy_top
)
38 from sympy
import __version__
as sympy_version
39 python_version
= "%d.%d.%d" % tuple(sys
.version_info
[:3])
42 """Puts ">>> " in front of each line."""
44 for l
in s
.split("\n"):
50 "Python %s console for SymPy %s. These commands were executed:\n%s\nDocumentation can be found at http://sympy.org/\n" % \
51 (python_version
, sympy_version
, indent(init_code
))
53 def run_ipython_interpreter():
55 from IPython
.Shell
import IPShellEmbed
57 #is the -nopprint necessary?
60 ipshell
= IPShellEmbed(args
)
61 api
= ipshell
.IP
.getapi()
63 api
.ex('__IP.compile("from __future__ import division", "<input>", "single") in __IP.user_ns')
65 ### create some magic commands
67 #def pretty_print(self, arg):
68 # self.api.ex("print str((%s).__pretty__())" % arg)
70 #api.expose_magic("pprint", pretty_print)
72 # Now start an embedded ipython.
74 sys
.exit("Exiting ...")
76 def run_python_interpreter():
78 Couldn't locate IPython. Having IPython installed is greatly recommended. See
79 http://ipython.scipy.org for more details. If you use Debian/Ubuntu, just
80 install the "ipython" package and start isympy again.\n"""
88 class HistoryConsole(code
.InteractiveConsole
):
89 def __init__(self
, locals=None, filename
="<console>",
90 histfile
=os
.path
.expanduser("~/.sympy-history")):
91 code
.InteractiveConsole
.__init
__(self
)
92 self
.init_history(histfile
)
93 self
.runcode(self
.compile("from __future__ import division",
96 def init_history(self
, histfile
):
97 readline
.parse_and_bind("tab: complete")
98 if hasattr(readline
, "read_history_file"):
100 readline
.read_history_file(histfile
)
103 atexit
.register(self
.save_history
, histfile
)
105 def save_history(self
, histfile
):
106 readline
.write_history_file(histfile
)
108 sh
= HistoryConsole()
109 sh
.runcode(init_code
)
110 sh
.interact(welcome_msg
)
111 sys
.exit("Exiting ...")
114 from optparse
import OptionParser
117 from sympy
import __version__
as sympy_version
118 parser
= OptionParser("usage: isympy [options]", version
=sympy_version
)
119 parser
.add_option("-c", "--console", dest
="console",
120 help="specify a python interactive interpreter, python or ipython")
121 (options
, args
) = parser
.parse_args()
123 if options
.console
== "python":
124 run_python_interpreter()
125 elif options
.console
== "ipython":
126 run_ipython_interpreter()
129 run_ipython_interpreter()
132 run_python_interpreter()
134 if __name__
== "__main__":