Python Learning Notes-module introduction (i)-module concept and basic use

Source: Internet
Author: User

Python is composed of a series of modules, each module is a py suffix file, and the module is also a namespace, thus avoiding the problem of variable name conflict. Module we can be understood as Lib library, if you need to use a module in a function or object, you want to import this module can be used, in addition to the system default module does not need to import outside. The import directly uses the following syntax:

Import Module name (do not. py suffix)

Once the import succeeds, you can use the function or object of the imported module in another module. For example, create a Python module in the D:\temp directory: module_1.py, which reads as follows:

#-*-coding:utf-8-*-' module_1.py module contents ' ' Name = ' Ironwood box ' SiteURL = ' http://www.mzone.cc '

The first line above is to specify the encoding format, because Python is processed by default in ASCII encoding, so it is not possible to process non-English languages, and by specifying the encoding you can achieve internationalization effects. The second line is the comment information, annotated with "'. Then, we go through the D:\temp directory into the python command-line mode, so that the current directory (D:\TEMP) as a working directory, so we can successfully find the Module_1 this module, as follows:

D:\temp>pythonpython 2.7 (r27:82525, Jul 4, 09:01:59) [MSC v.1500 + bit (Intel)] on Win32type "help", "copyright "," credits "or" license "for more information.>>>

At this point, if we enter print directly (SiteURL), the system will error, the SiteURL variable is undefined:

>>> print (SiteURL); Traceback (most recent call last): File "<stdin>", line 1, in <module>nameerror:name ' SiteURL ' are not defined& Gt;>>

So we need to import the contents of Module_1 to define the SiteURL variable, but note that the variables of the imported module are not defined in the top-level namespace, but in the namespace of the module, so the following methods are used to import the following print variables:

>>>import Module_1>>>print (Module_1.siteurl)

If you use Print directly (SiteURL), you still report siteurl undefined errors because of the namespace issues mentioned above. If you want to use the variable directly in the top-level namespace, you can use the following import method:

>>>from module_1 Import siteurl>>>print (SiteURL)

This allows the SiteURL variable in the Module_1 module to be imported into the top-level namespace, and the direct use of the variable will not be an error. Of course, we can also rename the imported variables as follows:

>>>from module_1 Import SiteURL as Myurl>>>print (Myurl)

This allows the value of the SiteURL to be assigned to the Myurl variable, because the From...import statement is used, so the variable is bound to the top-level namespace, and we can use the variable name directly.

It is important to note whether you use the import or the From: Import the way the module is imported, is actually telling the Python interpreter to load the specified module and executing all the statements in the module, so if there is a print-like statement in the module, we will also see the output of these statements during the import process.

For each module's import, the Python interpreter is imported only once, even if the import and From...import statements are reused, and only if PVM detects that the module is not imported. Even if later you modified the source code of the module, but did not restart the Pvm,python interpreter is still using the previously imported content in processing. If you need to reload the modified source code, one is to exit the Python interactive mode and then enter, and the second is to directly use the reload statement, as follows:

>>>reload (module_1) <module ' module_1 ' from ' module_1.py ' >

We can see that the system prompts to reload the source file of the Module_1 module, and after we have modified the content, we can use this method to re-import and then execute it to see the modified content.


Python Learning Notes-module introduction (i)-module concept and basic use

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.