0

I need to get the location of an installed package (i.e. from "site-packages" or "dist-packages"), but I may be inside the package's directory itself.

I have tried everything from both of these questions, and some problems inevitably arise.

I have tried pip.get_installed_distributions(), and this is very close, but there is no guarantee that the package will have been installed with pip. If the package is not installed with pip, then get_installed_distributions() doesn't return the package.

Community
  • 1
  • 1
Travis
  • 2,579
  • 18
  • 19

1 Answers1

0

How about:

python -c 'import module; print module.__file__'

E.g:

$ python -c 'import ssl; print ssl.__file__'
/usr/lib64/python2.7/ssl.pyc

Compare to:

$ python -c 'import requests; print requests.__file__'
/home/lars/env/common/lib/python2.7/site-packages/requests/__init__.pyc
larsks
  • 277,717
  • 41
  • 399
  • 399
  • This doesn't work if I am inside the package directory. In other words, if I clone a package locally, and try to import the package while inside that directory, doing those commands will just give me the local location. – Travis Jun 11 '15 at 15:39
  • Isn't that the correct behavior, though? It's telling you the location of the module that Python would use if you were to run it from that directory. Alternatively, you could `(cd /; python -c 'import module; print module.__file__')`. Stick that in an alias and you're all set... – larsks Jun 11 '15 at 17:35
  • I will try to do this through Python (moving to root first), and see it that does the trick. It seems like a roundabout way to achieve this, but if it works it works – Travis Jun 12 '15 at 14:46