turn fromhttp://www.cnitblog.com/seeyeah/archive/2009/03/15/55440.html
The following will introduce several commonly used situations:
(1) The main program as the module program is in the same directory:
As the following program structure:
`– src
|– mod1.py
`– test1.py
If you import module mod1 in the program test1.py, directly use the import mod1 or from mod1 import *;
(2) The directory where the main program is located is the father (or ancestor) of the directory where the module is located
As the following program structure:
`– src
|– mod1.py
|– mod2
| `– mod2.py
`– test1.py
If you import module MOD2 in the program test1.py, you need to establish an empty file in the MOD2 folder __init__.py file (you can also customize the output module interface in this file); then use the from mod2.mod2 import * or or or Import Mod2.mod2.
(3) The module under the module or other directory (level) in the upper directory of the main program
As the following program structure:
`– src
|– mod1.py
|– mod2
| `– mod2.py
|– sub
| `– test2.py
`– test1.py
If you import module MOD1 and MOD2 in the program test2.py. First of all, the __init__.py file (with (2)) is required under the MOD2. There is no need to establish the file under SRC. Then the call method is as follows:
The following program execution methods are executed in the directory where the program file is located.
and test1.py are Python Test1.py after CD SRC; then the Python Sub/Test2.py is successful in the src directory.
import sys
sys.path.append(“..”)
import mod1
import mod2.mod2
(4) From (3), it can be seen that the key to the import module is to find the path of the specific module according to the value of the Sys.path environment variable. Here are only three simple situations above.
From: http://hi.chinaunix.net/? 253639/viewSpace- 25422