Import in Python

From Wikinico

Jump to: navigation, search

Contents

What is import ?

When a program is really long to write, you split it in several files, each of them taking charge of a part of the program. It is easier to manage than one big monolithic file.

When you a piece of your program is generic enough to be reused in other programs, it would be a waste of time not to reuse it. In this case, by separating this piece of the program in one or a subset of the files of the program, you'll be able to recall them later in another program without interfering with the other parts of your original program.

In both case you need a mechanism to use the code written in one file to be used in an other file, this is provided by the import command.

There are some rules to make your files reachable by import by that's not the point here.

The point is to understand:

import => make code written in (an) external file(s) avalaible here

How to use import ?

import os
import os.path
import os.path as way
import os.path as path
from os import path
from os.path import ...

Classic modules to import

import os
import sys

In Maya

import maya.cmds
import maya.cmds as mc
import maya.cmds as cmds
from maya import cmds
Personal tools