Importings Modules

We can import modules in 3 different methods

Import Method 1 

We import the module, but to use the features of the module we need to state the module name each time we use it.
1
2
3
4
import datetime
x = datetime.datetime.now()

print (x.year)

Import Method 2

We import a part of module, we limit the use of the module to the feature we imported. We would need to import any other modules we want to use separate.
1
2
3
4
from datetime import datetime
x = datetime.now()

print (x.year)

Import Method 3

We import the module, but we use a wildcard to import all features of the module. This is a dangerous way to import a module as this may clutter the namespace and replace features.
1
2
3
4
from datetime import *
x = datetime.now()

print (x.year)

Import Locations

Python will search the Python Path environmental variable location and the local location that the python file is located for the file to import. for example if a file existed in the same folder and the python file being executed called "mathtest.py", we can import this with "import mathtest". We can temporarily add locations to the python path with the following code.

1
2
3
import sys
sys.path.append("C:/path/to/Modules")
print sys.path



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines