error executing "import html.parser" from a script

P

Paul Volkov

What is this madness?
I have Python 3.3.0 installed on Windows XP. I do not have Python 2
(but I had it before). I do the following steps:

1. Import from an interactive session (no problems)
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
2. Import from command line (no problems)
python -c "import html.parser"

3. Import from a script. The script contains only one import line.
Everything else is commented out.
python e:\tmp\pyt\html.py
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\tmp\pyt\html.py", line 7, in <module>
import html.parser
File "e:\tmp\pyt\html.py", line 7, in <module>
import html.parser
ImportError: No module named 'html.parser'; html is not a package

4. And then I tried to print sys.path from my script by modifying it this way:
import sys
print (sys.path)
import html.parser

And the result is: (I don't know why sys.path is printed twice)
python e:\tmp\pyt/html.py
['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
D:\\Python33', 'D:\\Python33\\lib\\site-packages']
['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
D:\\Python33', 'D:\\Python33\\lib\\site-packages']
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\tmp\pyt/html.py", line 8, in <module>
import html.parser
File "e:\tmp\pyt\html.py", line 8, in <module>
import html.parser
ImportError: No module named 'html.parser'; html is not a package
 
H

Hans Mulder

What is this madness?

That's because your script is called "html.py".

If you import html.parser, Python first imports html,
then checks that it's a package and contains a module
named "parser". When Python imports html, it searches
for a file named "html.py". It finds your script,
imports it and decides that it's not a package.

Solution: rename your script.
I have Python 3.3.0 installed on Windows XP. I do not have Python 2
(but I had it before). I do the following steps:

1. Import from an interactive session (no problems)
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
2. Import from command line (no problems)
python -c "import html.parser"

3. Import from a script. The script contains only one import line.
Everything else is commented out.
python e:\tmp\pyt\html.py
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\tmp\pyt\html.py", line 7, in <module>
import html.parser
File "e:\tmp\pyt\html.py", line 7, in <module>
import html.parser
ImportError: No module named 'html.parser'; html is not a package

4. And then I tried to print sys.path from my script by modifying it this way:
import sys
print (sys.path)
import html.parser

And the result is: (I don't know why sys.path is printed twice)

The first time, your script is being run as a script, and
has the module name '__main__'. The second time, your script
is being imported as a module, and has the module name 'html'.

When Python finds the command "import html.parser" for the
second time, there's an (incomplete) module named "html" in
sys.modules, and Python doesn't try to import html again
and instead tries to use it as a package, and fails.
python e:\tmp\pyt/html.py
['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
D:\\Python33', 'D:\\Python33\\lib\\site-packages']
['e:\\tmp\\pyt', 'D:\\WINDOWS\\system32\\python33.zip',
'D:\\Python33\\DLLs', 'D:\\Python33\\lib', '
D:\\Python33', 'D:\\Python33\\lib\\site-packages']
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "e:\tmp\pyt/html.py", line 8, in <module>
import html.parser
File "e:\tmp\pyt\html.py", line 8, in <module>
import html.parser
ImportError: No module named 'html.parser'; html is not a package

Notice how the import is also being reported twice.


Hope this helps,

-- HansM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,531
Members
45,004
Latest member
KetoBoostGummies

Latest Threads

Top