Python learns the concept of---templates/packages

Source: Internet
Author: User

1.1. Module /pack of Concept

In Python, a. py file is called a module

There are three types of modules:

Python Standard library

Third-party modules

Application Custom Modules

Use of modules: modules are used to organize functions

After the interpreter finds the hhh.py through the search path, it hhh.py all the memory after parsing and assigns all the values to the HHH variable, after which the value needs to be hhh.xxx ()

# import HHH      #解释器通过搜索路径找到hhh. P, after hhh.py parsing all add memory and all assigned to HHH this variable, after the value needs Hhh.xxx () # from HHH import Hello          # Import Hhh.hello (), you can directly use the print (Hello (str)) # from HHH import hello as world # Imports Hhh.hello () and renamed world# from HHH Import *              # If there is a local function with the same name, the result of the function will be different depending on the reference location

Use of the package: The package is used to organize the module, import HHH, is to call the HHH package and execute __init__.py

The import package simply executes the __init__.py and is not associated with the other modules

# The package import must be a module, different packages directly through the point to import # imports  hhh    #  is called HHH this package and executed __init__py This module, not associated with other modules # from DEMO.HHH import test      # from the HHH package below the demo package, import the test module # from the Demo.hhh.test import add  # from the HHH package below the demo package to import the Add method inside the test module # from demo import Test          # import test module from inside Demo package print (Add (4,5)) # import Demo.logger             # error Importerror:no module named ' Demo.logger ' # Import demo.hhh.test           # error Importerror:no module named ' Demo.logger '
1.2. the basedir in Python
# version:python3.2.5# Author: ' FTL1012 ' # TIME:2017/12/12 21:28import os, sys# relative path && absolute path base_dir = Os.path.dirn Ame (Os.path.dirname (Os.path.abspath (__file__)) # Add environment variable Sys.path.append (base_dir) # print (base_dir) # print (Sys.path ) from Atm.module Import main                       # cannot be executed in the Python interpreter, unable to find ATM package Main.main () # Print (__file__)                                    # f:/python_demountitled /atm/bin/bin.py# Print (Os.path.abspath (__file__))                   # f:\python_demountitled\atm\bin\bin.py# Print ( Os.path.dirname (Os.path.abspath (__file__))  # F:\Python_demountitled\ATM\bin
1.3.__name__ variable
if __name__ = = ' __main__ ':    pass# if within this function, the display is __main__# if it is called in another function, then the module name is displayed # for functional testing
1.4. directory Structure

Briefly explain: bin/: To store some executable files of the project, of course you can name script/and so on. foo/: Stores all source code for the project. (1) All modules and packages in the source code should be placed in this directory. Do not place the top level directory. (2) Its subdirectory tests/storage unit test code; (3) The entrance of the program is preferably named main.py. Docs/: Store some documents. setup.py: Scripts to install, deploy, and package. Requirements.txt: A list of external Python packages that store software dependencies.     easy for developers to maintain software package dependencies. Add the new packages in the development process to this list to avoid missing packages when the setup.py installation is dependent. Make     it easy for readers to identify which Python packages the project uses. README: project documentation, functions as follows:       software positioning, the basic functions of the software.       How to run your code: Installation environment, startup commands, and so on.       brief instructions for use.       Code directory Structure Description, more detailed can explain the basic principles of the software.       FAQ's description.

"Learning Reference" http://www.cnblogs.com/alex3714/articles/5161349.html

"Learning Reference" http://www.cnblogs.com/yuanchenqi/articles/5732581.html

"Learning Reference" http://www.cnblogs.com/alex3714/articles/5765046.html

Python learns the concept of---templates/packages

Related Article

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.