Python——Introducing the import method into the module

巴扎黑
Release: 2017-08-06 16:50:38
Original
1205 people have browsed it

This article mainly introduces relevant information that explains in detail the examples of modules introduced by the Python import method. In Python, use import or from...import or from...import...as... to import the corresponding module. Friends who need it can refer to it

Detailed explanation of examples of modules introduced by the Python import method

In Python, use import or from...import or from...import...as... to import the corresponding module. The functions and usage methods are the same as The include header file in C language is similar. In fact, it is to introduce some mature function libraries and mature methods to avoid reinventing the wheel and improve development speed.

Python's import method can introduce system modules, or we can introduce shared modules written by ourselves. This is very similar to PHP, but their specific details are not quite the same. Because php specifies the specific path of the imported file when importing, and python cannot write the file path for importing.

The following is a summary of several situations of import:

Python’s method of including modules in subdirectories is relatively simple. The key is to be able to find the path to the module in sys.path The path to the file.

The following will introduce several common situations in detail:

(1) The main program and the module program are in the same directory:

The program structure is as follows :


-- src 
|-- mod1.py 
– test1.py
Copy after login

If you import the module mod1 in the program test1.py, directly use import mod1 or from mod1 import *;

(2 ) The directory where the main program is located is the parent (or ancestor) directory of the directory where the module is located

The program structure is as follows:


-- src 
|-- mod1.py 
|-- mod2 
|– mod2.py 
`– test1.py
Copy after login

If To import the module mod2 in the program test1.py, you need to create an empty file init.py file in the mod2 folder (you can also customize the output module interface in this file); then use from mod2.mod2 import * or import mod2.mod2 .

(3) The main program imports modules in the upper directory or modules under other directories (horizontal)

The program structure is as follows:


-- src 
|-- mod1.py 
|-- mod2 
|– mod2.py 
|– sub 
| -- test2.py 
– test1.py
Copy after login

If you import the modules mod1 and mod2 in the program test2.py. First, you need to create the init.py file under mod2 (same as (2)). There is no need to create this file under src. Then the calling method is as follows:

The following program execution methods are all executed in the directory where the program file is located. For example, test2.py is executed after cd sub; python test2.py

And test1.py is executed after cd src; python test1.py; is not guaranteed to be successful in executing python sub/test2.py in the src directory.


import sys 
sys.path.append(“..”) 
import mod1 
import mod2.mod2
Copy after login

The above is the detailed content of Python——Introducing the import method into the module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!