2

I need to find out the path for a particular library in my script.

I tried using :

os.path.dirname(MODULE.__file__),

but it gives an attribute error:

"type object 'MODULE' has no attribute  '__file__'."

Is there any other way to find the path for a particular module? I do not want to use sys.path as it gives the list of all the libraries.

user812786
  • 4,302
  • 5
  • 38
  • 50
  • 1
    it is (MODULE.__file__), and not (MODULE._ _file__)- I had to give space between the underscores due to some error. – user7073454 Oct 26 '16 at 07:05
  • I just submitted an edit to remove the spaces in `__file__` - If you format your code correctly (backticks for inline code, four spaces indent for code blocks) the underscores will display correctly and not cause italics/bold. – user812786 Oct 26 '16 at 12:36

5 Answers5

1

You have space between underscores _ _ in MODULE.__file__. Call it like:

>>> import os, operator
>>> os.path.dirname(operator.__file__) # where MODULE is "operator"
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4'
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You can use the both dirname() and basename() if you are going to split the passed filename into components without taking into account the current directory. If you want to also consider the current directory, you have to do so explicitly.

To get the dirname of the absolute path, use

os.path.dirname(os.path.abspath(__file__))

Note: Remove the spaces before file. As the response of @paxdiablo here, when a module is loaded in Python, __file__ is set to its name. You can then use that with other functions to find the directory that the file is located in.

Community
  • 1
  • 1
Kian
  • 1,319
  • 1
  • 13
  • 23
0

It looks like you are not examining a module, but rather a type object. You might try:

os.path.dirname(MODULE.__module__.__file__)

caveat

Usually you can use the __file__ attribute to find out where the module is located, but as stated in the docs (you have to scroll down a bit or ctrl + f and search for __file__) this attribute is not present.

The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

You can however use the imp module. It has the find_module function which will perform the same search that is performed when you actually import the module. This would lead to code like this:

import imp

data = imp.find_module("MODULE")
# The path of the module is the second element of the tuple
pathname = data[1]
iLoveTux
  • 3,552
  • 23
  • 31
0

Use os module

os.path.dirname(<module>.__file__)

An example:

>>> import os, requests
>>>
>>> requests.__file__
'/usr/lib/python2.7/site-packages/requests/__init__.pyc'
>>>
>>> os.path.dirname(requests.__file__)
'/usr/lib/python2.7/site-packages/requests'
>>>
>>> os.path.abspath(requests.__file__)
'/usr/lib/python2.7/site-packages/requests/__init__.pyc'

os.path.dirname - just removes the last segment in the path i.e. __init__.pyc - in short it returns the project root.

Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
  • I have something like : " from numpy import NaN" If I want to find the location of NaN, "os.path.dirname(Nan.__file__)" will give an error "no module called Nan! How to find location of Nan? – user7073454 Nov 10 '16 at 06:49
0

Actually my code was something like : from <module> import blah

I was trying to find the module path using : os.path.dirname(module.__file__) It gave an error saying "module" does not exist. So I did : import module and then from <module> import blah Then it worked !!