Python: Get Script Path

By Xah Lee. Date: . Last updated: .

Get path of current running script

import os

# path of current script
print(os.path.realpath(__file__))

Get Python interpreter path

import sys

# path of the python interpreter
print(sys.executable)
# C:\Python39\python.exe
import sys

print(sys.version)
# 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]

Get a module's path

moduleName.__file__

import os
print(os.__file__)

# /Users/xah/anaconda3/lib/python3.7/os.py

Python, Paths and Module

Python, Read Write File