# ! / Usr / bin / env Python # - * - Coding: UTF-8 - * - # **************************** ******* definitions and the import module *********************************** # modules: in Python, a file is called a .py module (Module1); assigning a code related to the code module in energy may make it easier to understand. # Module is divided into three categories: 1.python standard library; 2 third-party modules; 3 custom application modules. # Module Notes: 1. improve code maintainability; 2 finished writing a module from elsewhere call; 3. module name and keyword repetition should be avoided # 1 module reference method (import module) # Import Time, SYS, OS #import import module # Import Test # Print (test.my_add (2,8)) # From Test # Import my_add introducing a certain specific function or method from the module # Print (my_add (2,8)) # From Import Test Method # * all functions or introducing module (all introduced generally not recommended, it is possible to cause the same name) # 2. Supplementary module # (1) when introduced into a python interpreter module is looked up according to a certain route, find use sys.path can view the path # (2) If you want to add a path to a permanent search path module in added directly in the operating system environment variables; use the list append method to add sys.path is a temporary path # Import SYS # Print (sys.path) # 3. In various import module package # package: is a folder containing an __init__.py file, which defines the application execution environment python by n or n sub-module packet thereof; module can be used to organize the package ; avoid conflicts # that package is a directory that contains the file __init__.py, the catalog got to have the __init__.py files and other modules or sub-package # will be the first time (1) to import a package: Notes execution __init__.py file, and then import the package will be # from my_test import my_test1 # Print (my_test1.my_pingfang (9)) # Between from my_test.my_test1 import my_pingfang # multi-level directory. Connections to import # Print (my_pingfang (. 9)) # (2) "__ name__" == "__main__" # Generally, we will put the main program if "__name__" == "__main__" : under, when directly execute a .py file, the file then __name__ == '__main__' is True # Print ( "the py has been executed") # iF __name__ == "__main__": # Print ( "the ok") # Print (__ name__) # when a file is called py, being py file called by __name__ value is the name of our py file, that judgment false, this time debugging code can be placed under if, will not be executed # from my_test Import my_test1 # (3) "__ file__": If the module is called from a directory two directories, you need to join a directory sys.path can normally call # Note: absolute path / # Import os, SYS # Print (__ file__ ) #__file__ can print the current path py file, pycharm execution result is E: / python_code / basics / common basis for the concept of modules / module package .py # based on the concept cmd execution results module package .py # Print (os.path.dirname (__ file__)) # print the absolute path of the current directory on the file level # Absolute path print (os.path.abspath (__ file__)) # print the current file # Print (os.path.dirname (os.path.dirname (os.path.abspath (__ file__)))) # print a directory of absolute path # From the module to call a secondary directory directory # Import OS, SYS # abs_dir = os.path.dirname (os.path.dirname (os.path.abspath with (__ file__))) # sys.path.append (abs_dir)
Based on the concept of modules
Guess you like
Origin www.cnblogs.com/shichenyang/p/12065644.html
Ranking