3

(See this question for what the m means)

I need to construct the include path of .virtualenvs/foo/include/pythonX.Ym to compile something (i.e. -I...) against the virtualenv. I can get the X.Y using sys.version or sys.final_version.

How do I get the m to construct the include path?

EDIT: I tried sys.executable but that is pointing to .../foo/bin/python, which is unhelpful for this.

Community
  • 1
  • 1
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121

1 Answers1

5

The easiest way to get the include path is to use the sysconfig.get_path() function:

import sysconfig

include_path = sysconfig.get_path('include')

This path is adjusted for virtualenvs already. For scripting purposes outside of Python, you can either print the path directly:

$ python -c 'import sysconfig; print(sysconfig.get_path("include"))'

or get all sysconfig data by running the module as a script:

$ python -m sysconfig

then parse the output that dumps to stdout.

Other than that, if you only want the executable name (with the m included), you can get that from the sys.executable variable; this includes the m suffix:

>>> import sys
>>> sys.executable
'/usr/bin/python3.5m'

As of Python 3.2, you can also use the sys.abiflags variable; it is set to m in this case:

>>> sys.abiflags
'm'

Also see PEP 3149.

For earlier Python versions, the various flags that influence the suffixes are available still via the aforementioned sysconfig module as configuration variables:

pymalloc = bool(sysconfig.get_config_var('WITH_PYMALLOC'))
pydebug = bool(sysconfig.get_config_var('WITH_PYDEBUG'))
wideunicode = bool(sysconfig.get_config_var('WITH_WIDE_UNICODE'))

Note that ubuntu merely compiles multiple binaries and adjusts the executable name to reflect the configuration option chosen; on other systems the ABI flags are not necessarily reflected in the executable name.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343