How to make pip3 work inside Flatpak sandbox?

Hi,

Anyone know how to make Python and pip3 work inside Flatpak?
I am experimenting with Spyder Flatpak package.

I have issue with installing extra library in Spyder after installation. pip3 install new libs but could not import it afterward.

Here I create a simple manifest to test on.


app-id: org.test.test
runtime: org.freedesktop.Sdk
runtime-version: 22.08
sdk: org.freedesktop.Sdk
command: test
finish-args:
  - --share=ipc
  - --socket=wayland
  - --socket=fallback-x11
  - --device=dri
  - --share=network
  - --filesystem=home
modules:
  - name: test
    buildsystem: simple
    build-commands:
      - mkdir /app/bin && touch /app/bin/test

Install with
flatpak-builder build --force-clean --install --user org.test.test.yaml

Running and importing dill library failed.

kevin@localhost:~/Desktop/test_pip3> flatpak run --command=bash org.test.test
[📦 org.test.test test_pip3]$ pip3 install dill
Defaulting to user installation because normal site-packages is not writeable
Collecting dill
  Using cached dill-0.3.6-py3-none-any.whl (110 kB)
Installing collected packages: dill
Successfully installed dill-0.3.6
[📦 org.test.test test_pip3]$ python3
Python 3.10.11 (main, Nov 10 2011, 15:00:00) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
FileNotFoundError: [Errno 2] No such file or directory: '/etc/pythonstart'
>>> import dill
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dill'

pip3 also does not see dill that it just installed

[📦 org.test.test test_pip3]$ pip3 list
Package    Version
---------- -------
Cython     0.29.35
Mako       1.2.4
Markdown   3.4.3
MarkupSafe 2.1.2
meson      0.63.3
pip        23.1.2
Pygments   2.15.1
setuptools 67.8.0
six        1.16.0
wheel      0.38.4

Any one have any idea?

After some digging it seems like there have something to do with sys.path.
An attempt to add path to sys.path with PYTHONPATH env does not work.

[📦 org.test.test test_pip3]$ export PYTHONPATH=/home/kevin/.local/lib/python3.10/site-packages:$PYTHONPATH
[📦 org.test.test test_pip3]$ python
Python 3.10.11 (main, Nov 10 2011, 15:00:00) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
FileNotFoundError: [Errno 2] No such file or directory: '/etc/pythonstart'
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/app/lib/python3.10/site-packages', '/usr/lib/python3.10/site-packages']
>>>

This seems to be a bug in the runtime:

I don’t know Python therefore I can’t help much. But the package was found with pip list --path <site-package PATH>, maybe it helps in finding a workaround.

Thank for pointing it out. I have found the work around here [WIP] Add org.spyder ide.spyder by kevinsmia1939 · Pull Request #4344 · flathub/flathub · GitHub