© Paul Gerrard 2016
Paul GerrardLean Python10.1007/978-1-4842-2385-7_5

5. Using Modules

Paul Gerrard
(1)
Maidenhead, Berkshire, UK
 
Once you have more than a hundred (or several hundred) lines of code in one Python file, it can be a little messy to manage all the functions and classes in the same place. Splitting your code over two or more module files that each cover one aspect of the functionality can simplify matters greatly.
The features and functions in the standard library (and other libraries that you might download and use from time to time) are made available to your programs as modules using the import statement.
Whether you are using your own home-grown modules or the standard libraries, the mechanism for including the code is the same.

Importing Code from a Module

The import statement has this format:
import modulename [as name]
This statement imports the module modulename. The optional 'as name' part allows you to reference that module with a different name in your code. If this statement works without error, then all of the functions and classes in that module are available for use.

Modules Come from the Python Path

When Python encounters an import modulename statement, it looks for a file called modulename.py to load.1 It doesn’t look just anywhere. Python has an internal variable called the Python path. You can see what it is by examining the sys.path variable.
The following interaction shows the path on a Windows machine.
>>> import sys
>>> sys.path
['', 'C:\Windows\SYSTEM32\python34.zip', 'c:\Python34\DLLs', 'c:\Python34\lib', 'c:\Python34', 'c:\Python34\lib\site-packages']
>>>
The Python path is a list of directories that is set up by the Python installation process, but you can also access and change the path to suit your circumstances.
When you install other libraries (e.g., from PyPI) the path might be updated. If you keep all your Python code in one place (in the same, current directory), you will never need to change the Python path. You only need to worry about the Python path if you are dealing with many modules in different locations.

Creating and Using Your Own Modules

Let’s suppose that you have a module with Python code called mod1.py; the name of the module in your code will be mod1. You also have a program file called testmod.py. Let’s look at some example code in each file.
This is mod1.py:
def hello():
    print('hello')
writtenby='Paul'
class greeting():
    def morning(self):
        print('Good Morning!')
    def evening(self):
        print('Good Evening!')
Now, suppose we have a test program mod1test1.py:
import mod1 as m1
print(writtenby)
m1.hello()
print('written by', m1.writtenby)
greet = m1.greeting()
greet.morning()
greet.evening()
Now, you can see in line 1 that we have imported mod1 as m1. This means that the code in module mod1 is referenced using the m1 prefix. If we had just imported the module, we would use the prefix mod1. When we run this code, we get the following result:
D:LeanPythonprograms>python mod1test1.py
hello
written by Paul
Good Morning!
Good Evening!
Let’s look at another test file (mod1test2.py).
from mod1 import greeting,hello,writtenby
hello()
hello.writtenby='xxx'
print('written by', hello.writtenby)
print(writtenby)
greet = greeting()
greet.morning()
greet.evening()
In this case we have imported the function and class using this format:
from module import function1, function2…
When we run this code, we get the following result:
D:LeanPythonprograms>python mod1test2.py
hello
written by xxx
Paul
Good Morning!
Good Evening!
You can see that the import format allows us to drop the prefix for the imported functions.
We now import only what we want and we also can name the imported functions and classes without the prefix. We could also have used the format:
from module import *
In this alternative we import all the functions and classes from the module. The result would have been the same for mod1test2.py; there would be no need for prefixes.
When we import from the standard libraries, which often have a large number of functions and classes, it is more efficient to work this way.
Note
In general, name your imported modules and import only what you need; that is, avoid "import *".
Footnotes
1
The import statement assumes your module files end with '.py'.
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.5.57