It is highly OS dependent. On Linux, because of the unusual X selection model, the easiest way is to use popen('xsel -pi')and write text to this channel.
For example: (I think)
def select_xsel(text):
import subprocess
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
xsel_proc.communicate(some_text)
As pointed out in the comments, on a Mac you can use a command /usr/bin/pbcopylike:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
, os.name, , :
import os, subprocess
def select_text(text):
if os.name == "posix":
try:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
except:
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
elif os.name == "nt":