如何为 snap 包设置 PYTHONPATH?

如何为 snap 包设置 PYTHONPATH?

我正在尝试对非纯 Python 项目进行 snap 打包。我使用了python带有自定义构建/安装脚本的插件。

该项目包含桌面应用程序所需的 2 个 Python 包:

  • alfanous(API)
  • alfanous-desktop(Qt PySide 图形用户界面)

snapcraft.yaml文件:

name: alfanous
version: "0.7.8"
summary: Alfanous
description: GNU hello prints a friendly greeting.
  This is part of the snapcraft tour at http://snapcraft.io/create/
#confinement: strict
confinement: devmode

apps:
  alfanous-gui:
    command: usr/bin/alfanous-desktop
    #desktop: usr/share/applications/my-app.desktop
  alfanous-cli:
    command: usr/bin/alfanous-console

parts:
  alfanous-git:
    build-packages:
      #- python
      - sqlite3
      - pyside-tools
      - qt4-linguist-tools
      - python-babel
      - qt4-qmake
      - python-setuptools
      #- python-pyparsing
      #- perl
    #plugin: make
    plugin: python
    python-version: python2
    #source: https://github.com/Alfanous-team/alfanous/archive/0.7.8.tar.gz
    source: https://github.com/Alfanous-team/alfanous.git
    source-type: git
    source-depth: 1
    #source-tag: 0.7.8
    build: |
      make build
    install: |
      echo _______#######:$(pwd)
      #make install_api 
      make install_api DESTDIR=$SNAPCRAFT_PART_INSTALL
      make install_desktop DESTDIR=$SNAPCRAFT_PART_INSTALL
    stage-packages:
      - python-pyparsing
      - python-pyside
      #- epydoc
      #- sphinx
      - python-babel
      - python-setuptools
    python-packages:
      - pyparsing
      - pyside
      - babel

snap 包构建和安装没有错误。但是,如果我尝试运行它:

$ alfanous.alfanous-cli
Traceback (most recent call last):
  File "/snap/alfanous/x3/usr/bin/alfanous-console", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2927, in <module>
    @_call_aside
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2913, in _call_aside
    f(*args, **kwargs)
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2940, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 635, in _build_master
    ws.require(__requires__)
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 943, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/snap/alfanous/x3/usr/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 829, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'alfanous==0.7.29' distribution was not found and is required by the application

我的第一个怀疑是不完整的PYTHONPATH,试图调试:

snap run --shell alfanous.alfanous-cli
env | grep -i python

什么也没带来。

并且两个包都在那里:

$ ls /snap/alfanous/current/usr/lib/python2.7/site-packages/
alfanous                        alfanousDesktop
alfanous-0.7.29-py2.7.egg-info  alfanousDesktop-0.7.29-py2.7.egg-info

我该如何PYTHONPATH正确设置?python 插件不应该处理这个问题吗?

snapcraft v2.28 Ubuntu 16.04.2 LTS 64位

答案1

我进行了修改alfanous-console以打印sys.path并重建快照:

['/snap/alfanous/x4/usr/bin',
 '/snap/alfanous/x4/usr/lib/python2.7',
 '/snap/alfanous/x4/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/snap/alfanous/x4/usr/lib/python2.7/lib-tk',
 '/snap/alfanous/x4/usr/lib/python2.7/lib-old',
 '/snap/alfanous/x4/usr/lib/python2.7/lib-dynload',
 '/snap/alfanous/x4/usr/lib/python2.7/dist-packages']

很明显,它snap不包括site-packagesPYTHONPATH的目标包自身安装的位置。

  • 所以我必须修改它的安装路径
  • 或者向 snap 团队报告以添加usr/lib/python2.7/site-packages

    有关的错误报告#1670749:经典限制需要手动设置 PATH 和 PYTHONPATH

    那里建议的解决方案不起作用:

    apps:
      alfanous-gui:
        command: usr/bin/alfanous-desktop
        environment:
          PATH: $SNAP/usr/bin:$SNAP/bin/:$PATH
          PYTHONPATH: $SNAP/usr/lib/python2.7/site-packages:$SNAP/usr/lib/python2.7/dist-packages:$PYTHONPATH
        #desktop: usr/share/applications/my-app.desktop
      alfanous-cli:
        command: usr/bin/alfanous-console
        environment:
          PATH: $SNAP/usr/bin:$SNAP/bin/:$PATH
          PYTHONPATH: $SNAP/usr/lib/python2.7/site-packages:$SNAP/usr/lib/python2.7/dist-packages:$PYTHONPATH
    

    由于指向主机/home文件夹的奇怪路径

    ['/snap/alfanous/x7/usr/bin',
     '/snap/alfanous/x7/usr/lib/python2.7/site-packages',
     '/snap/alfanous/x7/usr/lib/python2.7/dist-packages',
     '/usr/lib/python2.7/site-packages',
     '/usr/lib/python2.7/dist-packages',
     '/home/sneetsher/Desktop/sandbox/alfanous-snap',
     '/snap/alfanous/x7/usr/lib/python2.7',
     '/snap/alfanous/x7/usr/lib/python2.7/plat-x86_64-linux-gnu',
     '/snap/alfanous/x7/usr/lib/python2.7/lib-tk',
     '/snap/alfanous/x7/usr/lib/python2.7/lib-old',
     '/snap/alfanous/x7/usr/lib/python2.7/lib-dynload']
    

相关内容