Skip to content

4. Imported Modules

Barak Haim edited this page Apr 29, 2021 · 4 revisions

Imported Modules

import <module_name>

import <module_name> as <tmp_name>

from <module_name> import <func_or_class_name>

from <module_name> import *

  • random
  • datetime
  • math
  • sys
import time
t = time.time()

print(time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t)))

Importing Local Logic

Writing a file of functions or classes (or both) and separating the usage from the logics is considered to be a good practice. for example, imagine implementing a web-server API. On one file you write the server's functionality and on the other, the logic that would be used by the server. This separation makes our code cleaner and easier to navigate and edit. This also allows us to "take out" the logics part and reuse it somewhere else without having to deal with the web-server part.

Basically, When you have 2 .py files in the same directory, all you need to do is call the import statement on one file and say:

import <other_file_name_without_py_suffix>

Advanced import methodes.

Clone this wiki locally