Python Modules


(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


Python module is a normal python file which can store function, variable, classes, constants etc. Module helps us to organize related codes . For e.g math module in python has mathematical related functions.

Creating module

Create a new file called mymodule.py and write the following code.


as you can see we have defined a global variable foo  and a function hello()  in our module. Now to use this module in our programs we first need to import it using import statement like this

now you can use variable and call functions in the  mymodule.py  using the following code.

Expected Output:

Remember you need to specify name of module first to access it’s variables and functions, failure to so will result in error.

Using from  with import

Using import statements imports everything in the module, what if you want to access only specific function or variable ? This is where from  statement comes, here is how to use it.

Expected output:

Note: In this case you don’t need to specify module name to access variables and function.

dir() method

dir() is an in-built method used to find all attributes (i.e all available classes, functions, variables and constants ) of the object. As we have already discussed everything in python is object, we can use dir() method to find attributes of the module like this:

dir() returns a list of string containing the names of the available attributes.

As you can see besides foo and hello there are additional attributes in the mymodule . These are in-built attributes which python provides to all the modules automatically.

Congratulations you have completed all building blocks you need to master python !!


Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!


2 thoughts on “Python Modules

  1. JName

    Thank you so much, admin. I’ve tried other tutorials, such as the one at python.org (I’m sure it’s great) but only yours made enough sense to me. My future is brighter now, because of this path opening up. Currently a welder….

    This last lesson, though, stumps me just a little. I keep getting

    when I try to load the program file containing the code to import mymodule. I got around this by adding mymodule.py to …Python\Python37\DLLs. Now it works just fine, but surely this is not standard practice. How can I get this to work without needing to do that? Normally all tutorial files are ran from My Documents.

    My variables and system variables in Environmental Variables are fully fleshed with all folders having anything to do with Python37, so I don’t think it would be that, but I’m also a n00b.

    =================

    Also, I think dir() argument needs quotations, at least on Win7. When I try dir(mymodule) I kept getting

    NameError: name ‘mymodule’ is not defined

    which is what it gives me when strings aren’t quoted trying to print() something. If I try dir(“mymodule”) it gives me a long list of all the attributes associated with the little custom module.

    Thank you again,

    J

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *