简体   繁体   中英

Finding Python3 install location

For my project I'd like to be able to dynamically load and use a Python 3 shared library, if one is installed on the system (any version >=3.2). If more than one Python 3 version is installed, it would be nice to know which one is the default (ie the one that gets used whenever user types python3 in the terminal).

On Windows, Python install locations can be discovered via the registry, however, I am uncertain if the same can be done on MacOS and Linux. While python3 executable can typically be found on PATH, there seems to be no general way of locating Python's shared library?

Edit: To clarify, I am looking for libpython3.x.so (or libpython3.x.dylib), not the main python executable .

Also, I'd like the discovery method to be portable: it should work on different Linux distros, and for different package managers on MacOS, so saying eg "It's in /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/Python" doesn't cut it.

In shell on MacOS or Linux, type:

which python and/or which python3 to get your answer

Example on a VM on my machine (under Docker):

uwsgi@08c2ed391dae:/src/psf$ which python
/usr/bin/python
uwsgi@08c2ed391dae:/src/psf$ which python3
/usr/bin/python3

Have you tried:

''' 
    >>> import sys
    >>> sys.path
'''

That shows lib path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM