Module (Modules)

Source: Internet
Author: User

first, the introduction of modules

Modules: When you write larger applications, all the code is sure to be split into multiple files, which is easy to maintain, and the functions and objects that have been written are used in multiple programs without having to copy functions and objects into each program.

The module supports the above functions, in Python, a. py file forms a module through which you can invoke programs in other files.

Module Name: fibo.py

Path: E:\djcode\untitled
1 __author__ ' Administrator ' 2 def laugh (): 3     Print ' Hahahaha '

Introducing Modules:

1>>>ImportFibo#introduction of the Fibo module2>>>Fibo. Laugh ()3 Hahahaha4>>> Fibo.__name__5 'Fibo'6 7>>> fromFiboImportLaugh#introduced the Fibo module, the introduction of which is Fibo undefined8>>>Laugh ()9Hahahaha
Second, search path

Python searches the following path for the module it wants to find:

The folder where the program resides

Installation path of the standard library

The path that the operating system environment variable Pythonpath contains

If you have a custom module, or a downloaded module, you can place the appropriate path according to the situation so that Python can find it.

Third, dir () function

The built-in function dir () is used to find out which names are defined in the module, and it returns a list of ordered strings:

1>>>ImportFibo2>>>dir (Fibo)3['__author__','__builtins__','__doc__','__file__','__name__','__package__','Laugh',]

Note that it lists the names of all types: variables, modules, functions, and so on.

Four, package (Packages)

Package: You can put modules with similar functions in the same folder and form a module package. The folder must contain a __init__.py file that reminds Python that the folder is a module package and __init__.py can be an empty file.

Directory structure:

Untitled (E:\djcode\untitled)

Directory1

d1fibo.py

Package1

__init__.py

pk1fibo.py

pk2fibo.py

pk3fibo.py

pk4fibo.py

pk5fibo.py

fibo.py

Introduction of fibo.py module in E:\djcode\untitled directory

1  Import Fibo         # imports the Python interpreter is Fibo This module name, if access to the Fibo module function is required by the module name to access 2 >> > fibo.laugh ()3hahahaha45fromimport Laugh     # import python interpreter is Fibo all functions and objects within this module, but does not import Fibo This module name, so Fibo will not be defined 6 > >> Laugh ()7 hahahaha

In the E:\djcode\untitled directory under the introduction of the package (Package1) under the pk1fibo.py module:

1>>>ImportPackage1.pk1fibo2>>>Package1.PK1fibo.laugh ()3 Pk1fibohahahahahaha4 5>>> fromPackage1ImportPk1fibo,pk2fibo,pk3fibo,pk4fibo,pk5fibo6>>>Pk1fibo.laugh ()7 Pk1fibohahahahahaha8 9>>> fromPackage1Import*#when the __init__.py file under Package (Package1) is empty,
The import module is equivalent to the from Package1 import Pk1fibo,pk2fibo,pk3fibo,pk4fibo,pk5fibo,
However, the current __init__.py file contains __all__ = ["Pk1fibo", "Pk4fibo", "Pk5fibo"],
This is equivalent to the from Package1 import Pk1fibo, Pk4fibo,pk5fiboTen>>>Pk1fibo.laugh () One Pk1fibohahahahahaha A>>>Pk2fibo.laugh () - Traceback (most recent): -File"<input>", Line 1,inch<module> theNameerror:name'Pk2fibo' is notDefined

The d1fibo.py module under the introduction directory (Directory1) under the E:\djcode\untitled directory:

1 Import SYS 2 >>> sys.path.append ("Directory1")3from  Import  laugh4 >>> laugh ()5 D1fibohahahahahaha

fibo.py modules are introduced under the E:\djcode\untitled\ Directory1 directory:

1 Import SYS 2 >>> sys.path.append (".. " )3fromimport  laugh4 >>> laugh () 5 Hahahaha
v. Summary

From module Import function

Import Module

__init__.py

Module (Modules)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.