Modules in Python

Source: Internet
Author: User

1. How to make your own code a module. You only need to put the. py file in the specified folder to let the interpreter know where to find the module. You can set a directory in either of the following ways:

# Method 1: Edit sys. path: import syssys. path. append ('G: \ mymodule. in this folder, the interpreter can find # Method 2: add the directory of the storage module by setting the environment variable PYTHONPATH. You need to know how to set environment variables in linux and windows. # Hello. py, put it in the specified directory print ('hello, World') >>> import hello # import hello for the first time, world # the code in the module will be executed during the first import >>> import hello # The second import will not be executed. >>> Hello = reload (hello) # if you want it to execute the available reload statement

2. How to add test code to the module

# Solution 1: Dummies # hello. pydef hello (): print ('hello') # testhello () # This statement is also executed when the module is imported, if you do not want to use the second solution during import # the second solution, the recommended solution # hello. pydef hello (): print ('hello') # testdef test (): hello () if _ name __= '_ main _': test () # This sentence is useful

3. To organize modules, You can group them into packages ). The package is basically another type of module. The interesting thing is that it contains other modules. A package is the directory where the module is located. To treat python as a package, it must contain a file named _ init _. py ). To place the module in the package, you can directly put the module in the package directory.

# Package example: Assume that a package named drawing contains the shapes and colors modules and contains the following files:

\ Drawing \__ init _. py, \ drawing \ shapes. py, and \ drawing \ colors. py. When importing a package, there are three solutions:

Import drawing # Only _ init __. py is usable, and shapes and colors cannot be used import drawing. colors # colors is available, but it can only be drawing. colorsfrom drawing import shapes # shapes can be directly used

4. What are the modules?

(1) dir: You can use the dir function to view the content of a module. It lists all features of the module, such as functions, classes, and objects.

>>> [n for n in dir(copy) if not n.startswith('_')]['Error', 'PyStringMap', 'builtins', 'copy', 'deepcopy', 'dispatch_table', 'error', 'name', 't', 'weakref']

(2) _ all __: when the _ all _ variable is set in the module, only the functions specified by the variable are used when the "from copy import *" statement is used, other variables are blocked by this variable.

>>> Copy. _ all _ ['error', 'copy', 'democopy'] # Only three external interfaces are provided.

(3) _ doc __: some descriptions of the module.

(4) _ file __: locate the source code document in this way, and obtain it by checking sys. path.

>>> print (copy.__file__)D:\Program Files\python\lib\copy.py

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.