About the import of Python

Source: Internet
Author: User

Simply say python import with from...import .... (Python module)

Import the appropriate modules in Python using Import or From...import. The module actually has some functions and the class collection file, it can implement some corresponding function, when we need to use these functions, directly imports the corresponding module into our program, we can use. This is similar to the Include header file in the C language, in Python we import the modules we need with imports.

eg

12345678910 Import sysprint (' ================python import mode========================== ');p rint (' The command line arguments is : ') for I in Sys.argv:print (i) print (' \ n the python path ', sys.path) from sys import argv,path# import a specific member of print (' ============== ==python from import=================================== ') print (' Path: ', path)

If you want to use the name of all SYS modules, you can do this: from sys import *
Print (' path: ', path) from the above we can see simply:

############################
#导入mode, the difference between import and from...import is that it simply says:
# If you want to enter the ARGV variable directly into your program and don't want to play sys every time you use it,
# You can use: from sys import argv
# Generally, you should avoid using from. Import and use the import statement,
# because it will make your program easier to read and avoid conflicting names
###########################

About path search issues in Import

Similar to the header file, the module also needs the system's search path, the following command is the system default search path, when you import a module, the system will be in the following path list to search for the corresponding file.

1 Print (Sys.path) [' D:/xx/pythonserver/python31/code ', ' d:\\xx\\pythonserver\\python31\\lib\\idlelib ', ' C:\\WINDOWS \\system32\\python31.zip ', ' d:\\xx\\pythonserver\\python31\\dlls ', ' d:\\xx\\pythonserver\\python31\\lib ', ' d:\\xx \\PythonSERVER\\python31\\lib\\plat-win ', ' d:\\xx\\pythonserver\\python31 ', ' d:\\xx\\pythonserver\\python31\\lib \\site-packages ']

(from the example table, we can see that Python will first be found in the current working directory.)

If the corresponding content is not found, the error is:

123456 Import Syss Traceback (most recent): File "d:/xxx/xxx/xx/code/test.py", line, in <module>import Syssimporterror:no module named Syss

Of course, we can also add our own path to search. The Append method of the invocation list is: Import sys
Sys.path.append (' D:/xx/pythonserver/python31/code ') Another note: The above information is summarized in the following section: http://blog.sina.com.cn/s/blog_4b5039210100ennq.html

To create your own module

Before you create it, it's important to note that each Python module has its own __name__ (just as each object has its own __doc__). Through __name__ we can find out the name of each module, the general __name__ of the value of the kind: 11 is the main module name: "__main__" (can be understood as directly run the file), 2 those imported by the main module name of the module is: file name (do not add the following. py). It is very useful to have __name__, because we can execute those modules by the IF __name__ = = ' xxx ' judgment, those modules are not executed. In addition: Each Python program is also a module. It has the extension:. py extension.

Below, we use examples to illustrate:
First: We create the module: mymodel.py

123456789 #!/user/bin/python#filename:mymodel.pyversion = ' 1.0 ' def sayHello (): Print (' Hello world ') def modelname (): Return _ _name__# returns its own name #end of the model above statement note:

1 This module should be placed in the same directory as the program in which we entered it, or in one of the directories listed in Sys.path.

2 You've seen that it's nothing special compared to our normal Python program.

Then: We call this module in test.py: test.py

1234567891011 Import sys,mymodelsys.path.append (' D:/xx/pythonserver/python31/code ') #提供搜索路径print (__name__) #此处打印主模块的名称: __main__ Mymodel.sayhello () print (' Version ', mymodel.version) print (' Model Name ', Mymodel.modelname ()) #打印被导入模块的名称: MyModel We use the From: Import ... print (' ======================from.....import===================================== ') from MyModel import * Print (__name__) #此处打印主模块的名称: __main__sayhello () print (' version ', version) print (' Model name ', ModelName ()) #打印被导入模块的名 said : MyModel

Note the above statement:

1 We can import multiple modules by importing, separated by "," (comma).

2 note import and from: Import .....

# #本文章转载于: http://hi.baidu.com/fc_lamp/blog/item/8c330cb534dc25678b

About the import of Python

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.