29

Let's say Python is installed in the location

C:\TOOLS\COMMON\python\python252


I want to print this location in the output of my program. Please let me know can I do this.

1
  • 5
    "this is not a home work" - no need to be paranoid :-) Commented Aug 13, 2009 at 7:40

3 Answers 3

40

you can use

import sys, os
os.path.dirname(sys.executable)

but remember than in Unix systems the "installation" of a program is usually distributed along the following folders:

  • /usr/bin (this is what you'll probably get)
  • /usr/lib
  • /usr/share
  • etc.
Sign up to request clarification or add additional context in comments.

2 Comments

I forgot the "dirname", sorry ^_^ now it's fixed
there is like a million files in those folders, python is a mess because somehow its insanely complicated to configure which version you are using
7

Maybe either of these will satisfy you:

>>> import sys
>>> print(sys.prefix)
/usr
>>> print(sys.path)
['', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2',
'/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', 
'/usr/local/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages', 
'/usr/lib/python2.5/site-packages/Numeric', '/usr/lib/python2.5/site-packages/gst-0.10',
'/var/lib/python-support/python2.5', '/usr/lib/python2.5/site-packages/gtk-2.0', 
'/var/lib/python-support/python2.5/gtk-2.0']

1 Comment

>>> print(sys.prefix) in python > 3.6.3
4

Try:

>>> import sys
>>> print sys.prefix

See the documentation for the sys module for more details.

1 Comment

Worked for me on Windows, but not Linux, where it gave /usr instead of /usr/bin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.