Python creating modules and ways to import modules

Source: Internet
Author: User

The examples in this article describe the Python creation module and the method of module import. Share to everyone for your reference. The specific analysis is as follows:

The Python Learning manual reads:

Define modules, just use a text editor, enter some Python code into the text, and then save with a. py suffix name, any such file would be considered a Python module.

For example, the following code is entered into a file and can be viewed as a module:

?

1 2 3 4 def Printme (Var): print var if __name__ = = ' __main__ ': printme (1)

If input into a.py, then import a can be imported into the module.

Then executable A.printme (3), the screen can print out 3:

?

1 2 3 >>> A.printme (3) 3 >>>

A variable that is defined at the top of a module automatically becomes the property of the module. For example:

?

1 2 3 4 5 data=[1,2,3] def Printme (Var): print var if __name__ = ' __main__ ': printme (1)

The data variable is a property of the module. In fact, Printme is also an attribute, just a function.

The Introduction module example is as follows: (assuming data=[1,2,3] is not defined at this time)

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 >>> Import a >>> a.data Traceback (most recent call last): File "<pyshell#1>", line 1, in <mo dule> a.data attributeerror: ' Module ' object has no attribute ' data ' >>> reload (a) <module ' a ' from ' C:/pya . PYc ' > >>> a.data traceback (most recent call last): File "<pyshell#3>", line 1, in <module> A.D ATA attributeerror: ' Module ' object has no attribute ' data ' >>>

From the above hints you can see that the data property is undefined, then define data=[1,2,3 in the a.py file, reload a module, and output the Data property:

?

1 2 3 4 5 >>> Reload (a) <module ' a ' from ' c:/pya.py ' > >>> a.data [1, 2, 3] >>>

The reload function here can reload a module. If you change it in your module code, you need to reload it.

The above a.data is to access the properties in the module.

The example above is to import a file as a module.

In fact, Python's module import also has a richer content.

In addition to the module name, Python can import the specified directory path. The directory of Python code is called a package. Therefore, this type of import is referred to as package import. In fact, package import is a namespace that turns a directory on your computer into Python. The attribute is the subdirectory contained in the directory or the module file.

Look at the following example:

In my desktop there is a AA folder, which has a BB folder, BB Inside there are a.py this file.

Then in the AA and BB folder, place a __init__.py, after the import AA.BB.A on the command line, you can import module A.

I hope this article will help you with your Python programming.

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.