2017-11-21 12 views

答えて

1

それはそこに、任意の公式スペック/ドキュメントの一部、および、as shown by another答えではありませんので、 sysconfig/distutils.sysconfig.get_config_var()のいずれかの適切な変数が設定されていない場合があります。すべてのケースで確実に取得する唯一の方法は、

です。 、、正確にはをビルド(例えば、 sourcetreeのPythonの場合でも)、リファレンス実装に委譲することです。

distutilsには、コンパイラのライブラリパスを設定するロジックis located in distutils.commands.build_ext.finalize_options()があります。だから、このコードは、ビルドの副作用なしでそれを得るでしょう:

import distutils.command.build_ext #imports distutils.core, too 
d = distutils.core.Distribution() 
b = distutils.command.build_ext.build_ext(d) #or `d.get_command_class('build_ext')(d)', 
               # then it's enough to import distutils.core 
b.finalize_options() 
print b.library_dirs 

注こと:

  • 結果のリスト内のすべての場所が必ずしも存在しません。
  • setup.pysetuptoolsの場合は、代わりにsetuptools.Distributionsetuptools.command.build_extを使用してください。
  • setup()に結果に影響する値を渡す場合は、ここにDistributionに渡す必要があります。

あなたが合格する必要がある追加の値のセットは、同じ滞在すること、または次のメンテナが、他のビルダーに切り替えないという保証はありませんので。拡張機能を構築するとき、値が唯一

  • あなたが本当にこの値を取得することになっていないのように思える独立すべてで、必要とされている:
    • あなたは別のビルド機能を使用している場合は、むしろbuild_extをサブクラス化し、ビルド中にベースメソッドから値を取得する必要があります。

さて、私はこの特定のものではなく、リモート可能

ある認めます
0

以下は、実行中のPythonのlibpythonxx.so/pythonxx.libの(かなり長い)subroutine in skbuild.cmakerです。 CMakeでは、350行のModules/FindPythonLibs.cmakeがこのタスク専用です。

ディレクトリだけを取得前者の一部ががはるかに簡単です:

libdir = dustutils.sysconfig.get_config_var('LIBDIR') 
if sysconfig.get_config_var('MULTIARCH'): 
    masd = sysconfig.get_config_var('multiarchsubdir') 
    if masd: 
     if masd.startswith(os.sep): 
      masd = masd[len(os.sep):] 
     libdir = os.path.join(libdir, masd) 

if libdir is None: 
    libdir = os.path.abspath(os.path.join(
     sysconfig.get_config_var('LIBDEST'), "..", "libs")) 

def get_python_library(python_version): 
    """Get path to the python library associated with the current python 
    interpreter.""" 
    # determine direct path to libpython 
    python_library = sysconfig.get_config_var('LIBRARY') 

    # if static (or nonexistent), try to find a suitable dynamic libpython 
    if (python_library is None or 
      os.path.splitext(python_library)[1][-2:] == '.a'): 

     candidate_lib_prefixes = ['', 'lib'] 

     candidate_extensions = ['.lib', '.so', '.a'] 
     if sysconfig.get_config_var('WITH_DYLD'): 
      candidate_extensions.insert(0, '.dylib') 

     candidate_versions = [python_version] 
     if python_version: 
      candidate_versions.append('') 
      candidate_versions.insert(
       0, "".join(python_version.split(".")[:2])) 

     abiflags = getattr(sys, 'abiflags', '') 
     candidate_abiflags = [abiflags] 
     if abiflags: 
      candidate_abiflags.append('') 

     # Ensure the value injected by virtualenv is 
     # returned on windows. 
     # Because calling `sysconfig.get_config_var('multiarchsubdir')` 
     # returns an empty string on Linux, `du_sysconfig` is only used to 
     # get the value of `LIBDIR`. 
     libdir = du_sysconfig.get_config_var('LIBDIR') 
     if sysconfig.get_config_var('MULTIARCH'): 
      masd = sysconfig.get_config_var('multiarchsubdir') 
      if masd: 
       if masd.startswith(os.sep): 
        masd = masd[len(os.sep):] 
       libdir = os.path.join(libdir, masd) 

     if libdir is None: 
      libdir = os.path.abspath(os.path.join(
       sysconfig.get_config_var('LIBDEST'), "..", "libs")) 

     candidates = (
      os.path.join(
       libdir, 
       ''.join((pre, 'python', ver, abi, ext)) 
      ) 
      for (pre, ext, ver, abi) in itertools.product(
       candidate_lib_prefixes, 
       candidate_extensions, 
       candidate_versions, 
       candidate_abiflags 
      ) 
     ) 

     for candidate in candidates: 
      if os.path.exists(candidate): 
       # we found a (likely alternate) libpython 
       python_library = candidate 
       break 

    # TODO(opadron): what happens if we don't find a libpython? 

    return python_library 
関連する問題