Learning how to use modules is an essential part of being able to learn how to use Python. Any Python file ending in .py can be used as a Python module. You will also sometimes find modules that are compiled into .pyc or .pyo files as well. In this example, We have a prepared import_demo.pi file that demonstrates multiple methods to import modules, and/or their data attributes and functions. The most basic form of an import is to use import, the name of the module. And then you’ll be able to use data attributes like math.pi or math.tan, parentheses, some value, parentheses, to access either a data attribute of that math module or a tangent function of that math module.
#Use separate namespace for "math" module print('List of object names in main namespace:') print(dir()) import math print('List of object names in main namespace', end=' ') print('after "import math" has executed:') print(dir()) print('The value of pi is', math.pi) print('The tangent of 1 is', math.tan(1)) del(math) #Use separate aliased namespace for "math" module print('List of object names in main namespace:') print(dir()) import math as fun print('List of object names in main namespace', end=' ') print('after "import math as fun" has executed:') print(dir()) print('The value of pi is', fun.pi) print('The tangent of 1 is',fun.tan(1)) del(fun) #Import selectively into __main__ namespace print('List of object names in main namespace:') print(dir()) from math import pi, tan print('List of object names in main namespace',end=' ') print('after "from math import pi, tan" has executed:') print(dir()) print('The value of pi is', pi) print('The tangent of 1 is', tan(1)) del(pi) del(tan)
What we’ll see, in all the examples, is the use of “dir,” which will print a list of names in a particular namespace. If you don’t specify the namespace, it defaults to __main__ namespace, the normal default main namespace. We are going to go ahead and run this module. We’ll see what happens with this very first block of code. And we see the list of object names in that main namespace; and then the print out of dir parentheses, parentheses; and then what happens to the main namespace after import math has been executed. You’d notice that math is an additional name in the main namespace. And it attempts to access math.pi or math.tan parentheses, some value, parentheses, were successful. Going back to the example, after printing out the value of pi and the tangent of 1, the math name is deleted from the main namespace.
#Use separate namespace for "math" module print('List of object names in __main__ namespace:') print(dir()) import math print('List of object names in __main__ namespace', end=' ') print('after "import math" has executed:') print(dir()) print('The value of pi is', math.pi) print('The tangent of 1 is', math.tan(1)) del(math)
The output : List of object names in __main__ namespace: ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',] List of object names in __main__namespace after "import math" has executed: ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'math'] The value of pi is 3.141592653589793 The tangent of 1 is 1.5574077249023]
And then the list of object names in the main namespace will be printed out again. In the second example, the name of math is alias to a name of fun. So we will import the math module as fun and, when we look at the namespace, it’s going to have fun in it instead of math. And will access attributes like fun.pi and fun.tan (1), to use that data attribute or call that function. And you can see deleting that fun name once it’s completed. So going back to the shell, right after the first one executed, we see the second list of names, again back to the normal default, where the fun name was added to the list of names. And that it was successful in accessing pi and the tangent functions.
Now to avoid conflict with names that you might already have defined in your main namespace, you can use aliases, like we did before for the entire module. You can also do this selectively with aliases for things brought into your main namespace. So the next example here is from math import pie, as pie with an “e,” tan as tangent, spelled out, and then those names pie, with an “e,” and tangent all spelled out, would be able to be used directly in your program code to either access that attribute or call that function. And so we can see that back in the listing of the name spaces, how “pi” with an “e” and tangent spelled out, appeared and function just fine. Now there’s one more way that sometimes you see people do, and that is to use a wild card.
# Import selectively with aliases into __main__ namespace print('List of object names in __main__ namespace:') print(dir()) from math import pi as pie, tan as tangent print('List of object names in __main__ namespace after', end=' ') print('"from math import pi as pie, tan as tangent" has executed:') print(dir()) print('The value of pi is', pie) print('The tangent of 1 is', tangent(1)) del(pie) del(tangent) The output: List of object names in __main__ namespace: ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',] List of object names in __main__namespace after "from math import pi as pie, tan as tangent" has executed: ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'pie', 'tangent'] The value of pi is 3.141592653589793 The tangent of 1 is 1.5574077249023
In this last example, it’s from the math module import asterisk which pollutes, as they say, a namespace. Every name that was defined within that math module will now appear within the main name space. So you see that long listing of all those different things; tan and pi among many, many others. And particularly if you did not author that module, it’s not a good idea to import with a wild card not knowing how those names could actually end up overwriting names that you’d already defined. So you should avoid using the wild card import, unless you might be experimenting in your shell. So now that you’ve seen different ways of doing imports of modules, their data attributes, and their functions, let’s create a module of our own that will import something from a module, and then use it to perform some calculations.
#Unless experimenting in the shell avoid "wildcard imports" as every #name in the module's namespace will be in the __main__ namespace from math import * print('You want to avoid polluting the __main__ namespace like this:') print(dir()) The output : You want to avoid polluting the __main__ namespace like this: ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'acos', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf']
Let’s go to File – New File, to go create that code. Take advantage of pi from the math module. Now since pi is not likely to be defined in our main namespace it’s the only thing we’re going to need in this example. We might just start off with from math import pi. We will use, again, the input function, that was previously discussed, to obtain some information from our user. We’ll get the radius of our circle by prompting the user with input function, and a string prompt such as “What is the circle radius?”, add that space, close your quote, close your parentheses. Now the result of that input will be a string and radius.
We’ll need to convert that to a floating point number in order to do some of the calculations that we will print. And the first value will be “The area of the circle is”, and the second value will be our calculation. We use the float function, which is built into Python, to convert that radius to a floating point number; pi r squared is the area of the circle. So the float of the radius squared, raised to the second power there, times pi.
from math import pi radius = input('What is the circle radius? ') print('The area of the circle is', float(radius) ** 2 * pi)
So that’s radius squared times pi. We could also print the circumference using the same technique, where two times the radius, converted to a floating point number, times pi will give us that value. Before we can execute this, we need to Save this file or put that in a folder, like my scripts, and we might call this circle_demo.py. Click Save.
from math import pi radius = input('What is the circle radius? ') print('The area of the circle is', float(radius) ** 2 * pi) print('The circumference of the circle is', 2 * float(radius) * pi) The Output : What is the circle radius? 5 The area of the circle is 78.53981633974483 The circumference of the circle is 31.41592653589793