Importing modules into Python within linux

Previous topic - Next topic

henrylaw

I am writing a scripter program that will take text from the clipboard, do some reformatting and then insert it into a text frame. To do this I'm trying to import 'pyperscript'.  Environment is Linux Mint 22.3

I code

  import pyperscript

... and get the message

  ModuleNotFoundError: No module named 'pyperclip'

The module is installed; I have test programs, run from the command line, that find and execute it.

Reading up, I think I understand that the python interpreter in Scribus has its own "sys.path", so I wrote a simple routine which prints that out; right enough it doesn't contain /usr/lib/python3/dist-packages, which is where "pyperclip" lives.  It does contain /usr/lib/python3.12/site-packages, so I experimented with

  sudo ln -s /usr/lib/python3/dist-packages/pyperclip /usr/lib/python3.12/site-packages/pyperclip

(and had to create /usr/lib/python3.12/site-packages in the process). But the module is still not found.

Is there a way to do what I want to do?

henrylaw

Update. I tried creating /usr/lib/python3.12/site-packages/pyperclip and then hard linking the files in there to their equivalents in /usr/lib/python3/dist-packages/pyperclip

Still the module is not found.

henrylaw

Update: I wrote a small module (in another language, to make sure I wasn't using the built-in Python interpreter) which does nothing more than read the clipboard and print it to stdout. In my scripter program I then coded

import subprocess
clip_data = subprocess.run( '/home/henry/Perl/tryout/tryout',
                            shell=False,
                            capture_output=True,
                            text=True )
scribus.messageBox( 'stdout',clip_data.stdout )

Result? "FileNotFoundError: [Errno 2] No such file or directory: '/home/henry/Perl/tryout/tryout'"

... and that file definitely does exist: I can execute it from the command line, thus:

henry@prospero:~$ /home/henry/Perl/tryout/tryout
This is data from the clipboard

I give up, frankly.  I shall have to paste my clipboard output to a file, and then within my scripter program read that file back in again.  Bah.

a.l.e

... if you can wait, I will try to give you some insights tonight CET...

a.l.e

So, let's see what I can do for you.

First, in my experience scripts running inside of Scribus correctly import module that are (correctly) installed system wide.

Indeed, on my Debian Linux I have requests installed through apt and

import requests

response = requests.get('https://forums.scribus.net')
print(response.text)

runs in the Scribus console, and does return some text that is very similar to the main page of this forum.

So, if you want to easily use pyperclip in your script (But do you really need it? I believe the Scribus scripter API does have functions for copying and pasting...), you will need to properly install it properly.
I would not mix pip and your systems package manager.

Now, requests is something that I use so often, that I'm happy to have it installed on my machine (I just need not to forget to add it the requirements if I share the script...), but I'm not sure that you want install pyperclip globally or if Mint has it at all (Debian testing does have it).

Sadly, the next step is a bit steeper.

If you don't want to correctly install the package system wide, then you need a virtual environment. But it does not seem to be possible to access it, by simply activating it in a terminal and starting Scribus from the same terminal while the virtual environment is active.

But there is a way to get there.
Even if it is a bit convoluted:

  • Create your virtual environment as usual and install the package in there (I went for "python -m venv venv" and then "pip install", but other types of virtual environment might also work)
  • In your script, get a copy of the environment and remove the environment variable PYTHON_HOME, add VIRTUAL_ENV with the path to the venv, and add your venv's bin to the env copy PATH.
  • Start a subprocess that uses the copy of the environment as its env
  • Get the result of the sub process.
  • In my script, the secondary script does not access Scribus... but I don't recall, if it was not possible or I simply did not need it. Probably the first one.

Voilà.
The script is not published yet.
I think that it was not 100% finished and I got distracted by a squirrel running in front of my window...
But if you are interested in going the way I describe above, on Thursday I can try to create a sample script that shows how to do it (or publish the script that calculates the contour of an image... which is way cooler...)

Enjoy your evening!

henrylaw

Thank you very much for weighing in to help.  My response:

1. pyperclip is installed via apt:
dpkg -L python3-pyperclip
/.
/usr
/usr/lib
/usr/lib/python3
/usr/lib/python3/dist-packages
/usr/lib/python3/dist-packages/pyperclip
/usr/lib/python3/dist-packages/pyperclip/__init__.py
... snipped
/usr/share/doc/python3-pyperclip/copyright
"/usr/lib/python3/..." looks pretty system-wide, and yet it definitely isn't picked up by my script (see earlier posts).

2. I've scoured the scripter interface docs (Link here) and can find no function for copying and pasting.  I confess that I used the table of contents page on the LHS to select what I thought were likely sub-headings for such a function - I didn't search it all.  I'd love to just grep it but don't have a complete file.

It seems that my experience with 'pyperclip' (installed system-wide with apt) is different from yours with 'requests' (also installed system-wide with apt). But why?  Mint is Ubuntu-based and therefore Debian-based.

a.l.e

just tested: pasteObject() does paste the content of the clipboard into a text frame that is in edit mode...

... and, yes, I have /usr/lib/python3/dist-packages/requests ...