python模块如何导入解释器_python – 模块导入适用于解释器,但不适用于脚本

我正在开始在这个网站http://www.bigfatalien.com/?p=223上关注elementtree的教程,所以像往常一样,我在解释器上输入了参考脚本,然后我去了

import xml.etree.ElementTree as xml

解释器运行该命令就好并在intrepreter中使用“xml”没有问题我的IDE甚至在自动完成时显示该类的成员但是只要我在脚本上输入完全相同的行并尝试运行它,它说

object has no attribute ‘etree’

,这条线有效:

import xml

但如果我补充:

xml.etree.ElementTree = xml

并尝试运行那个不起作用的脚本,我尝试使用我的IDE(pyscripter)和IDLE,同样的行为.

什么事情发生在这里,这是什么行为,我从来没有读过任何“如何导入python”教程或书籍.我觉得我错过了一些明显的东西.

使用请求的错误消息进行更新

从IDLE 2.6提供的线路

2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]

[u'C:\\Users\\grillermo\\Desktop', 'C:\\Program Files\\PyScripter\\Lib\\rpyc-python2x.zip', 'C:\\Python26\\lib\\site-packages\\dropbox_client-1.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\simplejson-2.1.6-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\poster-0.8.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\oauth-1.0.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\nose-1.0.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\mechanize-0.2.5-py2.6.egg', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\lib\\site-packages\\Pythonwin', 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']

2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]

[u'C:\\Users\\grillermo\\Desktop', 'C:\\Program Files\\PyScripter\\Lib\\rpyc-python2x.zip', 'C:\\Python26\\lib\\site-packages\\dropbox_client-1.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\simplejson-2.1.6-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\poster-0.8.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\oauth-1.0.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\nose-1.0.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\mechanize-0.2.5-py2.6.egg', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\lib\\site-packages\\Pythonwin', 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']

Traceback (most recent call last):

File "C:\Users\grillermo\Desktop\xml.py", line 4, in

import xml.etree.ElementTree as et

File "C:\Users\grillermo\Desktop\xml.py", line 4, in

import xml.etree.ElementTree as et

ImportError: No module named etree.ElementTree

口译员

C:\>python

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on

win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import xml.etree.ElementTree as et

>>> print et.__file__

C:\Python26\lib\xml\etree\ElementTree.pyc

>>>

解决方法:

一个非常愚蠢的问题:你是否将脚本文件命名为xml.py?如果是这样,请不要这样…导入xml.anything将查找您的脚本文件!给脚本文件提供与要导入的模块相同的名称绝不是一个好主意.

更新回溯是你的朋友.请仔细阅读.如果您不知道如何阅读它们,请在问题中包含回溯.

我打赌你的看起来非常相似:

Traceback (most recent call last):

File "xml.py", line 4, in

import xml.etree.ElementTree as et

File "C:\junk\xml.py", line 4, in #### here's the culprit ####

import xml.etree.ElementTree as et

ImportError: No module named etree.ElementTree

除此以外:

设置仅包含以下行的脚本:

import sys

print sys.version

print sys.path

import xml.etree.ElementTree as et

print et.__file__

import xml.etree.ElementTree as xml

print xml.__file__

运行它,并显示所有输出…复制/粘贴到您的问题的编辑.

在您的计算机上重复以下解释器会话,并报告打印的内容:

C:\junk>\python26\python

Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on

win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import xml.etree.ElementTree as et

>>> print et.__file__

C:\python26\lib\xml\etree\ElementTree.pyc

>>>

标签:python,import,interpreter,elementtree

来源: https://codeday.me/bug/20190714/1456251.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值