How to copy expressions from the screen - sympy/sympy GitHub Wiki

Copy-and-paste can present some difficulties especially if expressions are long and wrap (with line breaks) on your terminal. When this happens you can either copy it and paste it into an editor and fix the line breaks. A cross platform solution that works nicely is pyperclip If you install this and then define the following in your pythonstartup.py file

import pyperclip
copy = lambda x: pyperclip.copy(str(x))
pcopy = lambda x: pyperclip.copy(capture(lambda:pprint(x)))
paste = lambda: pyperclip.paste()

then any expression x can be copied as copy(x) or the pretty-printed version as pcopy(x). When you paste what is placed on your clipboard it will not be wrapped. Your normally paste key-sequence will work. You can also use the paste() command:

>>> copy(Lt(1, x))
>>> paste()
'1 < x'

If you copied this expression from the screen -- not as a string -- and then paste it, you will get x > 1. This is a consequence of working in Python where the first value that the interpreter sees is the 1, not the SymPy object x, and it reverses the operation. Similar issues (including why typing or pasting 1/2 becomes 0.5) are discussed in the gotchas that are part of the SymPy documentation.