获取Python的LIB路径 [英] Get Python's LIB path
问题描述
我可以看到INCLUDE
路径是 sysconfig.get_path('include')
.
I can see that INCLUDE
path is sysconfig.get_path('include')
.
但是我看不到LIB
的任何相似值.
But I don't see any similar value for LIB
.
NumPy 将其完全硬编码为 为<在Windows中为c3>,在Windows中为get_config_var('LIBDIR')
(在Windows中未记录和丢失).
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?
推荐答案
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,
在所有情况下完全可靠地获取它的唯一方法(例如,甚至对于源树中的Python)是委托给引用实现.
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.
在distutils
中,为编译器设置库路径的逻辑位于distutils.commands.build_ext.finalize_options()
中.因此,此代码将在构建时获得任何副作用:
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
请注意:
- 结果列表中并非所有位置都必须存在.
- 如果您的
setup.py
是基于setuptools
的,请分别使用setuptools.Distribution
和setuptools.command.build_ext
. - 如果将任何会影响结果的值传递给
setup()
,则也必须在此处将它们传递给Distribution
.
- Not all locations in the resulting list necessarily exist.
- If your
setup.py
issetuptools
-based, usesetuptools.Distribution
andsetuptools.command.build_ext
instead, correspondingly. - If you pass any values to
setup()
that affect the result, you must pass them toDistribution
here, too.
因为不能保证您需要传递的其他值的集合将保持不变,否则下一个维护者将不会切换到另一个构建器 1 ;并且只有在构建扩展程序时才需要该值,
Since there are no guarantees that the set of the additional values you need to pass will stay the same, or that the next maintainer won't switch to another builder1; and the value is only needed when building an extension,
- 似乎您根本不应该完全独立获得此值:
- 如果您使用的是其他构建工具,则应该改用
build_ext
子类,并在构建过程中从基本方法获取值.
- 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.
1 好吧,我承认这一特殊的可能性很小
这篇关于获取Python的LIB路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- If you're using another build facility, you should rather subclass
- 如果您使用的是其他构建工具,则应该改用