module_paths.py
71 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
Jörgen Stenarson
|
r4937 | """Utility functions for finding modules | ||
Utility functions for finding modules on sys.path. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2011, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
# Stdlib imports | ||||
Alyssa Whitwell
|
r24460 | import importlib | ||
Jörgen Stenarson
|
r4937 | import os | ||
Nathan Goldbaum
|
r25582 | import sys | ||
Jörgen Stenarson
|
r4937 | |||
# Third-party imports | ||||
# Our own imports | ||||
#----------------------------------------------------------------------------- | ||||
# Globals and constants | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Local utilities | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
Alyssa Whitwell
|
r24460 | def find_mod(module_name): | ||
Jörgen Stenarson
|
r4937 | """ | ||
Alyssa Whitwell
|
r24460 | Find module `module_name` on sys.path, and return the path to module `module_name`. | ||
Jörgen Stenarson
|
r4937 | |||
Matthias Bussonnier
|
r26419 | - If `module_name` refers to a module directory, then return path to __init__ file. | ||
Alyssa Whitwell
|
r24460 | - If `module_name` is a directory without an __init__file, return None. | ||
- If module is missing or does not have a `.py` or `.pyw` extension, return None. | ||||
- Note that we are not interested in running bytecode. | ||||
- Otherwise, return the fill path of the module. | ||||
Jörgen Stenarson
|
r4937 | |||
Parameters | ||||
---------- | ||||
module_name : str | ||||
Matthias Bussonnier
|
r26419 | |||
Jörgen Stenarson
|
r4937 | Returns | ||
------- | ||||
Alyssa Whitwell
|
r24460 | module_path : str | ||
Path to module `module_name`, its __init__.py, or None, | ||||
depending on above conditions. | ||||
Jörgen Stenarson
|
r4937 | """ | ||
Nikita Kniazev
|
r27075 | spec = importlib.util.find_spec(module_name) | ||
module_path = spec.origin | ||||
Alyssa Whitwell
|
r24460 | if module_path is None: | ||
Nikita Kniazev
|
r27075 | if spec.loader in sys.meta_path: | ||
return spec.loader | ||||
Alyssa Whitwell
|
r24460 | return None | ||
else: | ||||
split_path = module_path.split(".") | ||||
Christopher Brown
|
r24836 | if split_path[-1] in ["py", "pyw"]: | ||
Alyssa Whitwell
|
r24460 | return module_path | ||
else: | ||||
return None | ||||