Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Python's LIB path

I can see that INCLUDE path is sysconfig.get_path('include').

But I don't see any similar value for LIB.

NumPy outright hardcodes it as os.path.join(sys.prefix, "libs") in Windows and get_config_var('LIBDIR') (not documented and missing in Windows) otherwise.

Is there a more supported way?

like image 461
ivan_pozdeev Avatar asked Nov 21 '17 21:11

ivan_pozdeev


1 Answers

Since it's not a part of any official spec/doc, and, as shown by another answer, there are cases when none of appropriate variables from sysconfig/distutils.sysconfig .get_config_var() are set,

the only way to reliably get it in all cases, exactly as a build would (e.g. even for a Python in the sourcetree) is to delegate to the reference implementation.

In distutils, the logic that sets the library path for a compiler is located in distutils.commands.build_ext.finalize_options(). So, this code would get it with no side effects on the build:

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

Note that:

  • Not all locations in the resulting list necessarily exist.
  • If your setup.py is setuptools-based, use setuptools.Distribution and setuptools.command.build_ext instead, correspondingly.
  • If you pass any values to setup() that affect the result, you must pass them to Distribution here, too.

Since there are no guarantees that the set of the additional values you need to pass will stay the same, and the value is only needed when building an extension,

  • it seems like you aren't really supposed to get this value independently at all:
    • If you're using another build facility, you should rather subclass build_ext and get the value from the base method during the build.
like image 177
ivan_pozdeev Avatar answered Oct 05 '22 14:10

ivan_pozdeev