Python file naming trap __python

Source: Internet
Author: User

Recently learning Python, learn the XML section, and follow the tutorials to get the code as follows:

From Xml.parsers.expat import Parsercreate

class Defaultsaxhandler (object):
    def start_element (self, name, ATTRS):
        print (' sax:start_element:%s, attrs:%s '% (name, str (attrs))

    def end_element (self, name):
        print ( ' Sax:end_element:%s '% name

    def char_data (self, text):
        print (' sax:char_data:%s '% text)

XML = R ' " XML version= "1.0"?>
<ol>
    <li><a href= "/python" >Python</a></li>
    <li><a href= "/ruby" >Ruby</a></li>
</ol> "

handler = Defaultsaxhandler ()
parser = parsercreate ()
parser. Startelementhandler = handler.start_element
parser. Endelementhandler = handler.end_element
parser. Characterdatahandler = Handler.char_data
parser. Parse (XML)

The results of the operation are as follows:

Traceback (most recent call last):
File "D:\workspace\python_test_project\test\xml.py", line 1, in <module>
From Xml.parsers.expat import parsercreate
File "D:\workspace\python_test_project\test\xml.py", line 1, in <module>
From Xml.parsers.expat import parsercreate
Importerror:no module named ' Xml.parsers '; ' XML ' is not a package

Miss Si, how can be wrong, a search on the internet to know that it is my own file name for XML cause, but why my own file name to XML will cause this error. This also has to do with Python lookup module order, Python looks for module in the following order:

1. First of all, determine if this module is built-in or built-in,
If so, introduce the built-in module, and if not, look for it in a list called Sys.path.
2. Sys.path is generated dynamically when Python scripts are executed, including the following 3 sections:
A. The location of the script execution, that is, the current path
B. Pythonpath in environmental variables, i.e.. bash_profile
C. dependencies when installing Python

And here the XML belongs to the third party library does not belong to the built-in module, then print my sys.pyth look down:

[' D:\\workspace\\python_test_project\\test '
' D:\\workspace\\python_test_project '
' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35-32\\dlls '
' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35-32\\lib '
' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35-32 '
' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35-32\\lib\\site-packages '
' C:\\users\\administrator\\appdata\\local\\programs\\python\\python35-32\\python35.zip ']

You can see that, as mentioned above, the first is the current directory, the name of the current directory I also named for XML, So the Python interpreter will try to find xml.parsers.expat from the current xml.py file, which of course cannot be found, so where is the parsers.expat of my system. Looked for the following path:

%pythonpath%\lib\xml\parsers

Now I want you to know why the XML module named XML in it is an error.

So you say, now that you say that when you introduce a Third-party module that is not built in, the file name cannot be the same as the Third-party module name that is used because Python introduces the module sequence, then the built-in module file name and the built-in module name will make an error.

Add the SYS built-in module to the code above, and then print Sys.path to name the file name SYS:

And then run, and we can see that there's no problem, which validates our argument that when you use a Third-party module in your code, the file name cannot be the same as the module, and there is no such limit when you use the built-in module, and there is something wrong with it.

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.