I have surfed online but still not sure when we do
import matplotlib.pyplot
matplotlib is a file ? and pyplot is a class ? Could some one explain how they are structured.
Also all libraries that we get, can we see code for them .In Pycharm I just need to press ctrl+left click. But will it work for lets say a base function like casting operator - int().
Also for above example for file matplotlib how can I see all the possible functions and classes in them , is there any such function ?
3 Answers 3
matplotlib is a package, when you you import it, to Python it looks like any module.
pyplot is a module as well, but it's a module inside the matplotlib package.
You can ask Python itself:
>>> from matplotlib import pyplot
>>> type(pyplot)
<class 'module'>
>>> import matplotlib
>>> type(matplotlib)
<class 'module'>
A class would just be a type:
>>> from matplotlib import Parameter
>>> type(Parameter)
<class 'type'>
And to see what a module (or a class) has to offer, use dir():
>>> dir(matplotlib)
['LooseVersion', 'MatplotlibDeprecationWarning', 'MutableMapping', 'Parameter', 'Path', 'RcParams', 'URL_REGEX', '_DATA_DOC_APPENDIX', '_DATA_DOC_TITLE', '_ExecInfo', '__bibtex__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_add_data_doc', '_all_deprecated', '_check_versions', '_cm', '_cm_listed', '_color_data', '_constrained_layout', '_create_tmp_config_or_cache_dir', '_deprecated_ignore_map', '_deprecated_map', '_deprecated_remain_as_none', '_ensure_handler', '_error_details_fmt', '_get_config_or_cache_dir', '_get_data_path', '_get_executable_info', '_get_xdg_cache_dir', '_get_xdg_config_dir', '_image', '_init_tests', '_label_from_arg', '_layoutbox', '_log', '_logged_cached', '_mathtext_data', '_open_file_or_url', '_path', '_preprocess_data', '_pylab_helpers', '_rc_params_in_file', '_replacer', '_version', 'afm', 'artist', 'atexit', 'axes', 'axis', 'backend_bases', 'backend_tools', 'backends', 'bezier', 'blocking_input', 'category', 'cbook', 'checkdep_dvipng', 'checkdep_ghostscript', 'checkdep_inkscape', 'checkdep_pdftops', 'checkdep_ps_distiller', 'checkdep_usetex', 'cm', 'collections', 'colorbar', 'colors', 'compare_versions', 'container', 'contextlib', 'contour', 'cycler', 'dates', 'dedent', 'defaultParams', 'default_test_modules', 'docstring', 'dviread', 'figure', 'font_manager', 'fontconfig_pattern', 'ft2font', 'functools', 'get_backend', 'get_cachedir', 'get_configdir', 'get_data_path', 'get_home', 'get_label', 'get_py2exe_datafiles', 'gridspec', 'image', 'importlib', 'inspect', 'interactive', 'is_interactive', 'is_url', 'legend', 'legend_handler', 'lines', 'locale', 'logging', 'markers', 'mathtext', 'matplotlib_fname', 'mlab', 'mplDeprecation', 'namedtuple', 'numpy', 'offsetbox', 'os', 'patches', 'path', 'pprint', 'projections', 'pyplot', 'quiver', 'rc', 'rcParams', 'rcParamsDefault', 'rcParamsOrig', 'rc_context', 'rc_file', 'rc_file_defaults', 'rc_params', 'rc_params_from_file', 'rcdefaults', 'rcsetup', 're', 'sanitize_sequence', 'scale', 'set_loglevel', 'shutil', 'spines', 'stackplot', 'streamplot', 'style', 'subprocess', 'sys', 'table', 'tempfile', 'test', 'texmanager', 'text', 'textpath', 'ticker', 'tight_bbox', 'tight_layout', 'tk_window_focus', 'transforms', 'tri', 'units', 'use', 'validate_backend', 'widgets']
2 Comments
import matplotlib
means that matplotlib is a "module", which is a namespace.
it also means that
matplotlib.pyplot is a module, because that's what the import statement does. It loads modules.
So pyplot is not a class. And Python classes should start with capital letters, too.
If this import succeeds, it means that "magically", python at runtime knows what matplotlib is, because python has a path of places in your filesystem to look. The list of places is determined when Python is installed.
If you think of it as matplotlib.py, you'll conceptually be correct. When you import a module, python will look for a file with that name in its path. As with most library packaging systems, the import statement could be abstracting some actual complexity. Big libraries can be split over multiple files and so on.
The module pyplot can only be found by first accessing matplotlib. This could be mapped to a subdirectory in the file system. Directories which contain sub-modules are marked via an __init__.py file and if you search to learn more about __init__.py files, you will learn about the topic of python modules.
The import statement you gave as an example lets you refer to matplotlib.pyplot to access actually useful things, such as functions and classes.
So if there was a function "show()" you could refer to
matplotlib.pyplot.show()
in your code
def show_chart():
matplotlib.pyplot.show() #made-up example
if you had instead done
from matplotlib import pyplot
you would refer to
pyplot.show()
which saves some typing.
if pyplot has a class "Chart", you could do
my_chart = pyplot.Chart()
PS there is a version of the python console called ipython If it is installed on your system, Pycharm will use it when you do Tools -> Python Console.
Try
>>> import datetime
>>> ??datetime
the ?? query tells you a lot about the module, although it actually just accesses documentation that you can also access in other ways, as you have discovered If that example doesn't work, ipython is not installed, so google for that. It has some very nice shortcuts.
Comments
You can get the detailed structure of the matplotlib project at https://github.com/matplotlib/matplotlib/tree/master/lib/matplotlib
Make sure that your IDE has path set to where you have installed the libraries.
intis not an operator. It is a type, a constructor ofintobjects. Just like if I wroteclass Integer: ...then used it likeInteger(something)