LoginSignup
3
3

More than 5 years have passed since last update.

LibreOfficeバンドルのPythonにUbuntu - Pythonのパッケージのパスを追加する

Last updated at Posted at 2018-02-17

防備録ですが、誰でもわかるようにできるだけ詳しく書きます。

環境
  Ubuntu Studio 17.10
  LibreOffice6.0

  Ubuntu
  Python 2.7.14(64bit)
  Python 3.6.3(64bit)

  LibreOffice 6.0
  Python 3.5.4(64bit)
   Verが少し違うのですが、現在の所問題がありません。(パッケージがインストールされているのはpython3.6フォルダではなくpython3フォルダの方でした。)

LibreOfficeのマクロを作成するためにPythonを始めたのですが、(まだ一からプログラムを作成できるほど知識はありません。ネットで見つけたプログラムを自分用に改造して使っています。)
UbuntuのPythonでは、動作するのにLibreOfficeのPythonではModuleNotFoundErrorが出るので、Ubuntu側のPythonからフォルダ・*.pyファイルをコピーして使用していました。(最近シンボリックリンクに変えてみましたが、これでも動作します。)

数日前にP--Qさんのブログ
 https://p--q.blogspot.jp/2015/06/linuxbean140438libreofficepython.html
「linuxBean14.04(38)LibreOfficeバンドルPythonにパッケージを追加」
を発見したので、参考にして「sites.pth」を/opt/libreoffice6.0/program/python-core-3.5.4/lib/site-packages に保存しましたが、動作しませんでした。

重要

「sites.pth」の
/usr/lib/python36.zip
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
を削除しました。これがあるとエラーになります。
薄々問題がありそうな気がしていたのですが、今迄問題にならなかったので放置していましたが、今日エラーが出て削除するとエラーがなくなりました。(今回の分だけですが。まだどうなるかわかりません。)

site-packages/READMEを読むとsite.pyを参照しろと言うことなので見ると、「lib/site-packages」はWindows用みたいです。
Unixでは「lib / python <version> / site-packagesを追加します。」とありますので、フォルダを作成します。

<version>の番号も問題です。
@chatrate さんのブログ「【Python】site-packagesのパスを確認する方法【pip install】」
https://qiita.com/chatrate/items/eb4b05cd1a6652529fd9
で、丁度このパスを調べる方法が書いてありました。

path
$ /opt/libreoffice6.0/program/python
・・・
>>> import site; site.getsitepackages()
['/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5/site-packages']

上記のフォルダに「sites.pth」を置くと

sites.pth
$ /opt/libreoffice6.0/program/python /opt/libreoffice6.0/program/python-core-3.5.4/lib/site.py
sys.path = [
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib',
    '/opt/libreoffice6.0/program',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/lib-dynload',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/lib-tk',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/site-packages',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/python35.zip',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5/plat-linux',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5/lib-dynload',
    '/opt/libreoffice6.0/program/python-core-3.5.4/lib/python3.5/site-packages',
    ~~'/usr/lib/python3.6',~~ <ーこれがあるとエラーになります
    ~~'/usr/lib/python3.6/lib-dynload',~~ <ーこれがあるとエラーになります
    '/home/ty/.local/lib/python3.6/site-packages',
    '/usr/local/lib/python3.6/dist-packages',
    '/usr/lib/python3/dist-packages',
    '/usr/lib/python3.6/dist-packages',
]
USER_BASE: '/home/ty/.local' (exists)
USER_SITE: '/home/ty/.local/lib/python3.5/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

LibreOfficeのPythonにパスが追加されました。
何故か?python3.6なのに、パッケージがインストールされているのは/usr/lib/python3/dist-packagesでした。

これで、今の所はLibreOfficeのPythonでも動作するようになりました。

$ pip3 install pandas
でPython3にインストールしPython3でimport出来るようになりましたが、LibreOfficeのPythonでは
ImportError: Missing required dependencies ['numpy']
(必要な依存関係がありません)
のエラーがでて、単純にパスが通っているだけでは駄目そうですね。
LibreOfficeのPythonは同じファイル名でも中身が違うのではないでしょうか。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3