52

Lets say I import a module. In order for me to make the best use of it, I would like to know what properties, methods, etc. that I can use. Is there a way to find that out?

As an example: Determining running programs in Python

In this line:

os.system('WMIC /OUTPUT:C:\ProcessList.txt PROCESS get Caption,Commandline,Processid')

Let's say I wanted to also print out the memory consumed by the processes. How do I find out if that's possible? And what would be the correct 'label' for it? (just as the author uses 'Commandline', 'ProcessId')

Similarly, in this:

import win32com.client
def find_process(name):
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(".", "root\cimv2")
    colItems = objSWbemServices.ExecQuery(
         "Select * from Win32_Process where Caption = '{0}'".format(name))
    return len(colItems)

print find_process("SciTE.exe")

How would I make the function also print out the memory consumed, the executable path, etc.?

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
ldmvcd
  • 968
  • 3
  • 10
  • 15
  • Possible duplicate of [listing all functions in a python module](http://stackoverflow.com/questions/139180/listing-all-functions-in-a-python-module) – Mr_and_Mrs_D Jan 24 '17 at 22:08

4 Answers4

73

As for Python modules, you can do

>>> import module
>>> help(module)

and you'll get a list of supported methods (more exactly, you get the docstring, which might not contain every single method). If you want that, you can use

>>> dir(module)

although now you'd just get a long list of all properties, methods, classes etc. in that module.

In your first example, you're calling an external program, though. Of course Python has no idea which features wmic.exe has. How should it?

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thanks! So in my first example, how would I go about it if I needed to know additional features that I have at my disposal. As an example, if I also wanted to print out the executable path? – ldmvcd Feb 24 '11 at 10:49
  • 3
    By reading the [documentation](http://msdn.microsoft.com/en-us/library/aa394531%28v=vs.85%29.aspx). – Tim Pietzcker Feb 24 '11 at 11:15
  • Thanks for pointing me to the documentation. But I fail to see how I can retrieve the additional properties that I want. What am I missing? – ldmvcd Feb 24 '11 at 13:19
  • @Tim I'm not sure if you had received a notification because I hadn't tagged you in the comment. Just in case :) – ldmvcd Feb 24 '11 at 14:51
  • Sorry, I can't help you here. My answer is about how to find out the properties of Python modules (which was what you had asked about). Your problem appears to be finding out what capabilities a certain Windows server tool has - about which I haven't the foggiest idea besides RTM. – Tim Pietzcker Feb 24 '11 at 16:14
  • That's ok. I think I figured it out. Thanks for your help anyway. – ldmvcd Feb 24 '11 at 16:32
  • help(module) is really what i was searching for. Thanks a lot! – Thiru Jan 17 '13 at 06:26
  • Thanks! This helped me determine the actual file location of an imported module. – ndmeiri Apr 21 '18 at 05:28
22

dir(module) returns the names of the attributes of the module

module.__dict__ is the mapping between the keys and the attributes objects themselves

module.__dict__.keys() and dir(module) are lists having the same elements, though they are not equals because the elements aren't in same order in them

it seems that help(module) iswhat you really need

eyquem
  • 26,771
  • 7
  • 38
  • 46
  • Thanks! module.__dict__ maps what kind of keys to the attribute objects? – ldmvcd Feb 24 '11 at 10:56
  • @ldmvcd I don't understand your question. The attributes of a module are objects, as all in Python. To be attainable and usable, objects are binded to names : "_Names refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the binding of that name established in the innermost function block containing the use._" doc . Keys of this binding are names of nearly arbitrary nature, see here what the names can be: (http://docs.python.org/reference/lexical_analysis.html#identifiers) – eyquem Feb 24 '11 at 11:49
  • @ldmvcd Do also `import array` , for example, and `print array.__dict__` and you'll see the keys.... – eyquem Feb 24 '11 at 11:49
  • You mentioned that module.__dict__ is a mapping between keys and the attribute objects. I meant to ask what does one mean by 'keys' in this context. I'm not sure why I'm confused, but if I understand correctly, the 'keys' are names that are bound to the attributes of a module so that the said attributes can be accessed and used? But the keys(names) are arbitrary in nature? – ldmvcd Feb 24 '11 at 13:26
  • @ldmvcd I used a plain answer to have space – eyquem Feb 24 '11 at 16:39
4

Python has a build in function called dir(). I'm not sure if this is what you are referring to, but fire up a interactive python console and type:

import datetime
dir(datetime)

This should give you a list of methods, properties and submodules

Dirk
  • 1,184
  • 6
  • 22
  • I understand. For my second example, I tried help(win32com.client) and it shows up a lot of information. But I want to narrow it down to finding out how to retrieve the executable path and memory consumed. How do I find out the names for those properties(are they called properties?)? – ldmvcd Feb 24 '11 at 11:00
2

@ldmvcd

Ok, excuse me, I think you are a beginner and you don't see to what fundamental notions I am refering.

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy

I don't understand why it is called "abstraction": for me an object is something real in the machine, a series of bits organized according certain rules to represent conceptual data or functionning.

Names refer to objects. Names are introduced by name binding operations. Each occurrence of a name in the program text refers to the binding of that name established in the innermost function block containing the use. http://docs.python.org/reference/executionmodel.html#naming-and-binding

.

A namespace is a mapping from names to objects. Most namespaces are currently implemented as Python dictionaries, but that’s normally not noticeable in any way (except for performance), and it may change in the future. Examples of namespaces are: the set of built-in names (containing functions such as abs(), and built-in exception names); the global names in a module; and the local names in a function invocation. In a sense the set of attributes of an object also form a namespace. http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

By the way, I use the word attribute for any name following a dot — for example, in the expression z.real, real is an attribute of the object z. Strictly speaking, references to names in modules are attribute references: in the expression modname.funcname, modname is a module object and funcname is an attribute of it. In this case there happens to be a straightforward mapping between the module’s attributes and the global names defined in the module: they share the same namespace! http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

Namespaces are created at different moments and have different lifetimes. http://docs.python.org/tutorial/classes.html#a-word-about-names-and-objects

.

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called main. http://docs.python.org/reference/executionmodel.html#naming-and-binding

.

Well, a Python programm is a big machine that plays with objects, references to these objects , names of these objects, and namespaces in which are binded the names and the objects , namespaces being implemented as dictionaries.

So, you're right: when I refer to keys , I refer to names being the keys in the diverse namespaces. Names are arbitrary or not , according if the objects they have been created to name are user's objects or built-in objects.

I give advise you to read thoroughly the parts

3.1. Objects , values and types http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy

and

4.1. Naming and binding http://docs.python.org/reference/executionmodel.html#naming-and-binding

eyquem
  • 26,771
  • 7
  • 38
  • 46