MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python: get Python Path, Script Path

Xah Lee,

How to get the python executable path from current script?

# -*- coding: utf-8 -*-
# python

import sys

# path of the python program
print sys.executable            # sample output on Windows C:\Python27\python.exe

http://docs.python.org/2/library/sys.html

How to get the path of current running script?

get it from the __file__ attribute.

# -*- coding: utf-8 -*-
# python

import os

# path of current script
print os.path.realpath(__file__) # sample output on Windows c:\Users\h3\my-print-path.py

http://docs.python.org/2/library/os.html

How to find a module's path?

# -*- coding: utf-8 -*-
# python

import os
print os.__file__               # sample output C:\Python27\lib\os.pyc

see also: Python & Perl: Using Modules/Packages/Library

blog comments powered by Disqus