Get python path linux

How do I find the location of my Python site-packages directory?

There are two types of site-packages directories, global and per user.

    Global site-packages («dist-packages») directories are listed in sys.path when you run:

 python -c 'import site; print(site.getsitepackages())' 

Caution: In virtual environments getsitepackages is not available with older versions of virtualenv , sys.path from above will list the virtualenv’s site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:

 python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])' 

Practical Tips

  • .__path__ lets you identify the location(s) of a specific package: (details)
 $ python -c "import setuptools as _; print(_.__path__)" ['/usr/lib/python2.7/dist-packages/setuptools'] 
 $ python3 -c "import os as _; print(_.__file__)" /usr/lib/python3.6/os.py 
 $ pip show pytest Name: pytest Version: 3.8.2 Summary: pytest: simple powerful testing with Python Home-page: https://docs.pytest.org/en/latest/ Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others Author-email: None License: MIT license Location: /home/peter/.local/lib/python3.4/site-packages Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy 

there is a site package directory in a virtualenv. You can get the directory for site-specific modules inside/outside virtualenv using python -c «from distutils.sysconfig import get_python_lib; print(get_python_lib())» (it works on both Python 2 and 3 too).

This should be in any basic tutorial on using python. I’ve been pythoning for years and didn’t know about the site module. Would’ve saved so many headaches.

I have suggested a change to Python’s official tutorial. Please see PR #16974 for details, and thanks for the suggestion @salotz!

>>> import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] 

(or just first item with site.getsitepackages()[0] )

This is good but, unfortunately, this function is available only from python2.7. So, nice solution if you are using python2.7 or above, but not working for python 2.6 and below

I get AttributeError: ‘module’ object has no attribute ‘getsitepackages’ when using with virtualenv (python 2.7.8), the distutils solution below works though.

@radtek: for venv’s, I detect venv via hasattr(sys,’real_prefix’) and then determine site packages heuristically from [p for p in sys.path if p.endswith(‘site-packages’)][-1] (plus check if there is one found before doing the [-1] .

@radtek It is a known bug that the site module does not work for virtualenv github.com/pypa/virtualenv/issues/355

  • outside of virtualenv — provides the path of global site-packages,
  • insidue a virtualenv — provides the virtualenv’s site-packages
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" 

Formatted for readability (rather than use as a one-liner), that looks like the following:

from distutils.sysconfig import get_python_lib print(get_python_lib()) 

Source: an very old version of «How to Install Django» documentation (though this is useful to more than just Django installation)

Читайте также:  Google disk linux ubuntu

You can use virtualenvwrapper, which has the command cdsitepackages , to directly change into the environment’s site-packages directory.

First comment does not apply anymore: this showed the correct location both outside and inside a virtualenv.

Did not work for me on Windows 10 using Linux bash, it returns /usr/lib/python3/dist-packages instead of /usr/lib/python3.5/dist-packages .

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" 

It will point you to /usr/lib/pythonX.X/dist-packages

This folder only contains packages your operating system has automatically installed for programs to run.

On ubuntu, the site-packages folder that contains packages installed via setup_tools\easy_install\pip will be in /usr/local/lib/pythonX.X/dist-packages

The second folder is probably the more useful one if the use case is related to installation or reading source code.

If you do not use Ubuntu, you are probably safe copy-pasting the first code box into the terminal.

If the asker is after a particular package’s location, module.__file__ is the better way. If they’re trying to install things… just use the tools.

‘/usr/lib/pythonX.X/dist-packages’ in site.getsitepackages() on Ubuntu (though it goes after /usr/local/. in the list). You only get something into /usr/local via sudo pip and you shouldn’t use sudo pip on Ubuntu unless you decided to make your own distribution: if you use sudo pip , it is your responsibility to make sure that all dependencies of the current and future python modules installed via sudo apt or sudo pip are compatible. Consider what problem virtualenv was created to solve

python -c «from distutils.sysconfig import get_python_lib; print get_python_lib()» , I get ModuleNotFoundError: No module named ‘requests_unixsocket’

This is what worked for me:

same, in OS X Mavericks , my home .local isn’t what I wanted it to find plus yeah its not really there anyways.

Its in my main /Library folder I had to manually navigate to it, for some reason the direct full path wasn’t working

how do i use the packages installed here? if i do virtualenv it complains that the package does not exist. how do i invoke the packages installed at a custom location?

This tells you where Python will look for user specific packages. Comments like «doesn’t even exist» and «isn’t what I wanted it to find» don’t make sense in this context.

Читайте также:  Linux mint sims 4

A modern stdlib way is using sysconfig module, available in version 2.7 and 3.2+. Unlike the current accepted answer, this method still works regardless of whether or not you have a virtual environment active.

Note: sysconfig (source) is not to be confused with the distutils.sysconfig submodule (source) mentioned in several other answers here. The latter is an entirely different module and it’s lacking the get_paths function discussed below. Additionally, distutils is deprecated in 3.10 and will be unavailable soon.

Python currently uses eight paths (docs):

  • stdlib: directory containing the standard Python library files that are not platform-specific.
  • platstdlib: directory containing the standard Python library files that are platform-specific.
  • platlib: directory for site-specific, platform-specific files.
  • purelib: directory for site-specific, non-platform-specific files.
  • include: directory for non-platform-specific header files.
  • platinclude: directory for platform-specific header files.
  • scripts: directory for script files.
  • data: directory for data files.

In most cases, users finding this question would be interested in the ‘purelib’ path (in some cases, you might be interested in ‘platlib’ too). The purelib path is where ordinary Python packages will be installed by tools like pip .

At system level, you’ll see something like this:

# Linux $ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))" /usr/local/lib/python3.8/site-packages # macOS (brew installed python3.8) $ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))" /usr/local/Cellar/python@3.8/3.8.3/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages # Windows C:\> py -c "import sysconfig; print(sysconfig.get_path('purelib'))" C:\Users\wim\AppData\Local\Programs\Python\Python38\Lib\site-packages 

With a venv, you’ll get something like this

# Linux /tmp/.venv/lib/python3.8/site-packages # macOS /private/tmp/.venv/lib/python3.8/site-packages # Windows C:\Users\wim\AppData\Local\Temp\.venv\Lib\site-packages 

The function sysconfig.get_paths() returns a dict of all of the relevant installation paths, example on Linux:

>>> import sysconfig >>> sysconfig.get_paths() 

A shell script is also available to display these details, which you can invoke by executing sysconfig as a module:

Addendum: What about Debian / Ubuntu?

As some commenters point out, the sysconfig results for Debian systems (and Ubuntu, as a derivative) are not accurate. When a user pip installs a package it will go into dist-packages not site-packages, as per Debian policies on Python packaging.

The root cause of the discrepancy is because Debian patch the distutils install layout, to correctly reflect their changes to the site, but they fail to patch the sysconfig module.

For example, on Ubuntu 20.04.4 LTS (Focal Fossa):

root@cb5e85f17c7f:/# python3 -m sysconfig | grep packages platlib = "/usr/lib/python3.8/site-packages" purelib = "/usr/lib/python3.8/site-packages" root@cb5e85f17c7f:/# python3 -m site | grep packages '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', USER_SITE: '/root/.local/lib/python3.8/site-packages' (doesn't exist) 

It looks like the patched Python installation that Debian/Ubuntu are distributing is a bit hacked up, and they will need to figure out a new plan for 3.12+ when distutils is completely unavailable. Probably, they will have to start patching sysconfig as well, since this is what pip will be using for install locations.

Читайте также:  Linux h264 to mp4

Источник

Find full path of the Python interpreter?

How do I find the full path of the currently running Python interpreter from within the currently executing Python script?

3 Answers 3

sys.executable contains full path of the currently running Python interpreter.

import sys print(sys.executable) 

This does not seem to work from scripts with a shebang /usr/bin/env python executed as env -i ./script . In that case it returns the current working directory.

@JohnFreeman: I tried this on a GNU/Linux box w/ GNU coreutils 8.4 (env) and Python 3.4.2. #!/usr/bin/env python3 will return the correct full binary path via sys.executable . Perhaps your OS or Python version behaves slightly differently.

Note that this will not return the name of the Python interpreter if Python is embedded in some application.

I tried this with the shebang for python2 and python3 and it printed the correct executable. I also tried with no shebang and called the script with the python and python3 commands and it printed the correct executable.

@mic_e, this may have been true in 2015, but I just tried it today and it behaves as expected (it returns the absolute file of the executable that embeds Python).

Just noting a different way of questionable usefulness, using os.environ :

import os python_executable_path = os.environ['_'] 
$ python -c "import os; print(os.environ['_'])" /usr/bin/python 

There are a few alternate ways to figure out the currently used python in Linux is:

Similarly On Windows with Cygwin will also result the same.

kuvivek@HOSTNAME ~ $ which python /usr/bin/python kuvivek@HOSTNAME ~ $ whereis python python: /usr/bin/python /usr/bin/python3.4 /usr/lib/python2.7 /usr/lib/python3.4 /usr/include/python2.7 /usr/include/python3.4m /usr/share/man/man1/python.1.gz kuvivek@HOSTNAME ~ $ which python3 /usr/bin/python3 kuvivek@HOSTNAME ~ $ command -v python /usr/bin/python kuvivek@HOSTNAME ~ $ type python python is hashed (/usr/bin/python) 

If you are already in the python shell. Try anyone of these. Note: This is an alternate way. Not the best pythonic way.

>>> import os >>> os.popen('which python').read() '/usr/bin/python\n' >>> >>> os.popen('type python').read() 'python is /usr/bin/python\n' >>> >>> os.popen('command -v python').read() '/usr/bin/python\n' >>> >>> 

If you are not sure of the actual path of the python command and is available in your system, Use the following command.

pi@osboxes:~ $ which python /usr/bin/python pi@osboxes:~ $ readlink -f $(which python) /usr/bin/python2.7 pi@osboxes:~ $ pi@osboxes:~ $ which python3 /usr/bin/python3 pi@osboxes:~ $ pi@osboxes:~ $ readlink -f $(which python3) /usr/bin/python3.7 pi@osboxes:~ $ 

Источник

Оцените статью
Adblock
detector