Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell what directory an imported library comes from in python?

Tags:

python

import

I'm trying to modify a python library that I downloaded and am using. But the changes I'm making aren't doing anything. So I suspect that python is importing a different copy of this library from somewhere else on the filesystem. So...

When I run import foolib in python, how can I tell where on the filesystem it's getting that library from?

like image 959
muudscope Avatar asked Apr 15 '10 18:04

muudscope


People also ask

How can I tell where python is importing from?

Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to know the location of built in modules.

Where is Python library path?

For most Linux environments, Python is installed under /usr/local , and the libraries can be found there. For Mac OS, the home directory is under /Library/Frameworks/Python. framework . PYTHONPATH is used to add directories to the path.

Where are imported modules stored in Python?

Usually in /lib/site-packages in your Python folder. (At least, on Windows.) You can use sys. path to find out what directories are searched for modules.


1 Answers

the correct answer is to use sys.modules... it works on everything, even sys. sys.modules is a dictionary where the keys are the imported names (modules or packages), and the values are their respective locations. here is some usage output from my Mac:

$ python
Python 2.5.1 (r251:54863, Feb  9 2009, 18:49:36) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, django, google
>>> sys.modules['sys']
<module 'sys' (built-in)>
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/os.pyc'>
>>> sys.modules['django']
<module 'django' from '/Library/Python/2.5/site-packages/Django-1.1.1-py2.5.egg/django/__init__.pyc'>
>>> sys.modules['google']
<module 'google' from '/usr/local/google_appengine/google/__init__.py'>
like image 85
wescpy Avatar answered Oct 04 '22 20:10

wescpy