Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

Tutorial 20: User-Built Modules

<< Previous Tutorial           Next Tutorial >>
On this page: importing a user-created module using import, calling the user-defined functions in the module.

Video Tutorial


Python 3 Changes

print(x,y) instead of print x, y

Video Summary

  • A module is something that allows you to reuse already written code across scripts.
  • A module typically contains sets of user-defined functions that you can then use as normal Python commands.
  • If you are running a module that you wrote yourself, the module script file must be in the working directory (also called CWD, "current working directory"). In this case, it is the same directory as the caller script. In the tutorial, both .py files are on the desktop.
  • To use a module and its function in a script, you must first import it. The syntax is import module-file-name. In the tutorial, because the module file is "ep34.py", the importing statement is import ep34.
  • After that, calling a function from the module file is done by prefixing the function name with the module name and a period. In the tutorial, the function test_add() in "ep34.py" is therefore called as ep34.test_add().

Learn More

  • The ability to import your own script as a module is infinitely useful, although it's a bit difficult to see here because Ed had to use very simple examples.
  • In theory, every Python script can be imported and used as a module. But there is a caveat:
    • Module names are subject to the same restrictions applied to variable names, which means:
    • It can only contain alpha-numeric characters and the underscore "_", which means:
    • Your Python script get_plural.py can readily be imported as a module: import get_plural, but
    • Ex5.palindrome.naraehan.py cannot. The import statement import Ex5.palindrome.naraehan will throw an error. You will have to change the file name first.
  • Suppose your Python script contains function definitions as well as non-function commands like this:
    def isVow(char):
        return char.lower() in 'aeiou'
    
    print(isVow('i'))
    print(isVow('y'))
    vowel.py 
    
    You CAN import it as a module just fine, but the problem is, importing it will trigger the two commands at the bottom to run, resulting in printed output. You typically don't want that: you import a module in order to gain access to the functions in the module only. This issue can be avoided by restructuring the module script as follows:
    def isVow(char):
        return char.lower() in 'aeiou'
    
    def main():
        print(isVow('i'))
        print(isVow('y'))
    	
    if __name__ == '__main__':
        main()
    vowel.py 
    
    You can simply think of __name__ == '__main__' as a check to see if the script is run as a main process or being imported as a module. Now, the two print commands, now under a void function named main(), will only run when the script is executed on its own.
  • So far, we have been importing user-defined modules from within another script. What about importing in your IDLE shell? You can do it provided your CWD is the directory where your module script is. If it is not, you will have to change your CWD to your module script's location before you can import.

Practice

Copy and save the following script as v_c_count.py in your usual Python script folder. Then, import it as a module in another Python script file, and use the two functions included.

Explore