Python import usage and differences with from... import

Source: Internet
Author: User

Python import usage and differences with from... import

This article mainly introduces the usage of Python import and the difference with from... import. This article is concise and clear and easy to understand. For more information, see

Use import or from... import in python to import the corresponding module. The module is actually a collection file of some functions and classes. It can implement some corresponding functions. When we need to use these functions, we can directly import the corresponding modules into our programs, we can use it. This is similar to the include header file in C language. In Python, we use import to import the required modules.

Eg:

The Code is as follows:

Import sys

Print ('= ========= ');

Print ('the command line arguments are :')

For I in sys. argv:

Print (I)

Print ('\ n The python path', sys. path)

From sys import argv, path # import a specific Member

Print ('= ==================== ')

Print ('path: ', path) # because the path member has been imported, sys. path is not required for reference.

If you want to use the names of all sys modules, you can:

The Code is as follows:

From sys import *

Print ('path: ', path)

We can see from the above:

############################

# Import modules. The difference between import and from... import is that:

# If you want to use argv in the program to represent sys. argv,

# Use: from sys import argv

# Generally, use the import statement instead of from .. import,

# This will make your program easier to read and avoid name conflicts

###########################

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.