A classic Linux + Python packaging + pipx confusion! Don't worry, I'm here to untangle this for you.
The Short Story:
You need to install pyperclip inside the pipx virtual environment where your program is installed. Use pipx inject to do this.
The Long Explanation:
Here's what's happening:
- Debian/Ubuntu/Mint's
apt package management: When you install python3-pyperclip using apt, it installs the pyperclip package system-wide, making it available for all Python 3 programs run directly on the system (e.g., python3 myscript.py). The package is installed in the system Python environment, usually located at /usr/lib/python3.x/dist-packages/ (where x is your Python 3 version, e.g., 9, 10, etc.).
pipx and isolated virtual environments: pipx is a package runner that isolates Python applications in their own virtual environments (venvs). When you installed your Python program with pipx, it created a new, separate Python environment just for that program. This venv is not using the system Python environment (/usr/lib/python3.x/dist-packages/). Instead, it has its own site-packages directory, e.g., ~/.local/pipx/venvs/your_program/lib/python3.x/site-packages/.
- The error: Your program, installed via
pipx, tries to import pyperclip, but can't find it. Why? Because pyperclip is installed in the system Python environment (via apt), not in the isolated pipx venv where your program lives.
Now, why can't you use pip? You're correct that on Debian-based systems, pip might interfere with apt-managed packages. pipx helps avoid this by using isolated venvs. However, in this case, you do need to install a dependency inside the pipx venv.
The Solution:
Use pipx inject to install pyperclip inside the pipx venv of your program:
pipx inject your_program pyperclip
Replace your_program with the actual name of the program you installed with pipx (the one that's failing due to the missing pyperclip).
pipx inject will:
- Locate the
pipx venv for your_program.
- Activate that venv.
- Install
pyperclip inside that venv using pip.
After running this command, your program should now find pyperclip and run without errors.
Why not just use --system-site-packages with pipx?
While you could create a pipx venv with --system-site-packages, which allows the venv to see system-installed packages (like pyperclip from apt), this defeats the purpose of isolation that pipx provides. By using pipx inject, you keep the venv isolated and explicitly manage dependencies for your program.
Bonus Tip:
To list all packages installed in a pipx venv, use:
pipx runpip your_program list
This helps you verify that pyperclip is now installed inside the venv.
Happy Pythoning on your Debian-based system!