when i import module using
import module_name
is it possible to see where in my hard disk is that module located?
You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported. 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.
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.
Packages support one more special attribute, __path__ . This is initialized to be a list containing the name of the directory holding the package's __init__.py before the code in that file is executed. This variable can be modified; doing so affects future searches for modules and subpackages contained in the package.
How can I easily find the path to a Windows PowerShell module? Use the Get-Module cmdlet and a wildcard character for the name, and select the Path property.
It is worth mentioning that packages have __file__
attribute which points to __init__.py
, they also have __path__
which points to the package directory. So you can use hasattr(module_name, '__path__') and module_name.__path__[0] or module_name.__file__
.
Example (in REPL):
import socket, SOAPpy # SOAPpy is a package
socket.__file__
# .../python2.5/socket.pyc
socket.__path__
# AttributeError: 'module' object has no attribute '__path__'
SOAPpy.__file__
# .../2.5/site-packages/SOAPpy/__init__.pyc
SOAPpy.__path__
# ['.../2.5/site-packages/SOAPpy']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With