Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting file path of imported module [duplicate]

Tags:

python

How can I get the file path of a module imported in python. I am using Linux (if it matters).

Eg: if I am in my home dir and import a module, it should return back the full path of my home directory.

like image 438
cnu Avatar asked Apr 08 '09 11:04

cnu


1 Answers

Modules and packages have a __file__ attribute that has its path information. If the module was imported relative to current working directory, you'll probably want to get its absolute path.

import os.path import my_module  print(os.path.abspath(my_module.__file__)) 
like image 140
Cristian Avatar answered Sep 29 '22 18:09

Cristian