Python Basics----Import module imports and calls to packages

Source: Internet
Author: User

Module Concept

In Python, a. py file is called a module. The biggest benefit of using modules to organize your code is that it greatly improves the maintainability of your code.

There are three types of modules: Python standard library, third-party module, application custom module.

Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name.

Module Import

Import statement

1 Import module1[, module2[,... Modulen]

When using the import statement, the Python interpreter searches through the path of the Sys.path.

Each import, the corresponding opening up memory space to store the contents of the import, but with the import of the file to open the memory space is independent of each other. For example, when import foo.py executes the source file (foo.py) code in the newly created namespace, all references or modifications to the global namespace are based on foo.py.

Attention:

A module can be imported multiple times at the current location, but only the first import executes the content, and the others are reference memory

Change the invocation name:

1 Import logging as log2 log.critical ("www")

From...import statements

1 from modname import name1[, name2[, ... Namen]]

This declaration does not import the entire ModuleName module into the current namespace, but only introduces the name1 or name2 individual inside it into the global symbol table of the module that executes the declaration.

Call of the package

To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

Each package directory will have a __init__.py file, this file must exist, otherwise, Python will treat this directory as a normal directory (folder), rather than a package. __init__.pyit can be an empty file, or it can have Python code, because __init__.py it is a module, and its module name is the name of the corresponding package. The calling package is the __init__.py file under the Execute package.

Cross-Package invocation (MODULEPACK1 package in the current directory):

1 from Modulepack1[.modulepack2] Import module

If the current directory can find the package to be called, you can directly from the package import module, if the current directory cannot find the package to be called, you need to add the absolute path of the package to Sys.path.

Import a package name is equivalent to executing the __init__ file under this package.

Example:

Import Hello is not found in nod1, you need to add the package path manually, if it is found because pycharm to add an unnecessary path, the command line execution prevail.

1 Import sys,os2 Base_dir=os.path.dirname (Os.path.dirname (Os.path.abspath (__file__))) #__file__获取执行文件相对路径, The entire act is taken up to the upper level of the list 3 sys.path.append (base_dir) #添加路径4 import Hello5 hello.hello1 ()

Module Call test

1 if __name__== ' __main__ ': #__name__方法判断2     print (' OK ')

The direct execution of print (__name__) in a PY module outputs __main__, indicating that the current file belongs to the execution file, and if a py module is imported from another py module, the print (__name__) in the PY module being imported is executed Will output the module name of the module being imported, then the if judgment will not succeed, so the code in the judgment will not be executed when importing.

Mainly used in the debugging function, that is, a module is called when the content is not executed, while debugging the module itself can smoothly execute the module content.

Python Basics----Import module imports and calls to 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.