Drew Verlee
Drew Verlee

Reputation: 1900

Python importing a module: how to track its orgin PYTHONPATH, sys, os

the module nosetests runs everywhere on my computer (it shouldn't it should only run in a few specified places). I guess this is because i have accidentally added the module nosetests to the PYTHONPATH either by putting it directly in either the dist-packages or site-packages or telling python to look for it permanently everytime.

I'm familiar with a few commands like find, import os, import sys and PYTHONPATH but i can't seem to find a way track down the culprit directory thats allowing this to happen.

something like

>>> find . -name "*nosetests"* -print

any help would be great.

Upvotes: 1

Views: 217

Answers (2)

alexis
alexis

Reputation: 50190

To solve your problem it's enough to examine nosetests.__file__, as @Adam suggested. But there's a more general way: the inspect module, which also works for classes and other objects that don't have a __file__ attribute.

import inspect, nosetests
print inspect.getsourcefile(nosetests)

Upvotes: 2

Adam
Adam

Reputation: 2334

Let's look at this example:

>>> import itertools
>>> print itertools.__file__
/usr/lib/python2.7/lib-dynload/itertools.so
>>> import string
>>> print string.__file__
/usr/lib/python2.7/string.pyc

Upvotes: 2

Related Questions