7

Psetuptools と pkg_resources を含むパッケージに取り組んでいます。このパッケージでは、インストール後にいくつかのバイナリをダウンロードして、専用のディレクトリ ( P/bin/) に配置する必要があります。

pkg_ressources.resource_filename絶対ディレクトリパスを取得するために使用しようとしています。(virtualenv を使用するため)

を使用したインストール中python setup.py installに、pkg_ressources.resource_filename は のようなパスを返しません/home/user/tests/venv/lib/python3.4/site-package/P/bin/が、実際のモジュールへのパスを返します/home/user/projects/P/P/bin/

個人的なプロジェクト ディレクトリ (モジュールを開発する場所) ではなく、インストール ディレクトリ (virtualenv 内) が必要なため、これは問題です。

を使用して pypi を通過しようとすると、pip install moduleによって返されるディレクトリはpkg_ressources.resource_filenameのような一時ファイル/tmp/pip-build-xxxxxxx/P/bin/であり、バイナリを配置する場所ではありません。

これが私のsetup.pyです:

from setuptools import setup
import os

from setuptools.command.install import install as _install
from pkg_resources import resource_filename


def post_install():
    """Get the binaries online, and give them the execution permission"""
    package_dir_bin = resource_filename('P', 'bin') # should be /home/user/tests/venv/lib/python3.4/site-package/P/bin/
    # package_dir_bin = resource_filename(Requirement.parse('P'), 'bin') # leads to same results
    put_binaries_in(package_dir_bin)
    os.system('chmod +x ' + package_dir_bin + '*')



class install(_install):
    # see http://stackoverflow.com/a/18159969

    def run(self):
        """Call superclass run method, then downloads the binaries"""
        _install.run(self)
        self.execute(post_install, args=[], msg=post_install.__doc__)


setup(
    cmdclass={'install': install},
    name = 'P',
    # many metadata
    package_dir = { 'P' : 'P'},
    package_data = {
        'P' : ['bin/*.txt'] # there is an empty txt file in bin directory
    },
)

インストール、クロスプラットフォーム、および互換性のある python 2 および 3 中にインストールディレクトリを取得する標準的な方法はありますか? そうでない場合は、どうすればよいですか?

4

2 に答える 2

2

回避策は、インストール中にリソースにアクセスするように設計されていないように見えるsite代わりに、パッケージを使用することです。pkg_resources

インストール中にインストールディレクトリを検出する関数は次のとおりです。

import os, sys, site

def binaries_directory():
    """Return the installation directory, or None"""
    if '--user' in sys.argv:
        paths = (site.getusersitepackages(),)
    else:
        py_version = '%s.%s' % (sys.version_info[0], sys.version_info[1])
        paths = (s % (py_version) for s in (
            sys.prefix + '/lib/python%s/dist-packages/',
            sys.prefix + '/lib/python%s/site-packages/',
            sys.prefix + '/local/lib/python%s/dist-packages/',
            sys.prefix + '/local/lib/python%s/site-packages/',
            '/Library/Python/%s/site-packages/',
        ))

    for path in paths:
        if os.path.exists(path):
            return path
    print('no installation path found', file=sys.stderr)
    return None

モジュールに関する既知のバグのため、virtualenv を使用してインストールする場合、このソリューションは Python 2.7 と互換性がありませんsite。(関連するSOを参照)

于 2016-03-24T16:25:11.270 に答える