1

I have a text file on a virtual machine (without a GUI) to which I am connected through ssh in VSCode. I want to write python code that reads some text from this file and copies this to the clipboard so that I can paste this text into the browser which I have open on my local machine.

I tried using pyperclip. Specifically, the code looks something like:

import pyperclip
...
pyperclip.copy(string)
...

I get the following error:

Pyperclip could not find a copy/paste mechanism for your system.

I have xclip and xsel installed on the virtual machine. I read answers where it states that pyperclip does not work because it copies to the clipboard and clipboard is part of the GUI which a VM does not have. My final aim is to have it copied so that I can just paste it into the browser by using Ctrl+V. Basically, I want an automatic python code that does the equivalent of using Ctrl+C in a remote file open in a VSCode window.

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

It might be less of a headache to just write your data to a temporary file and then read from that.

To try to debug your issue though, this is how pyperclip is checking your system to see if xsel or xclip exists. It's not finding them, so you're getting that error message.

try:
    from shutil import which as _executable_exists
except ImportError:
    # The "which" unix command finds where a command is.
    if platform.system() == 'Windows':
        WHICH_CMD = 'where'
    else:
        WHICH_CMD = 'which'

    def _executable_exists(name):
        return subprocess.call([WHICH_CMD, name],
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0

_executable_exists("xsel") and _executable_exists("xclip") are both returning false in your VM.

Reference: pyperclip/src/pyperclip/init.py Also check this other question

just a guy
  • 31
  • 2