python 3导入不起作用

罗曼·茹恩(Romain Jouin)

我是Python 3的新手,正在重写Python 2程序。我有以下文件系统:

|-00_programs / test.py
|-01_classes / class_scrapper.py

我想scrapper从文件中导入类class_scrapper

这里是class_scrapper.py

# -*- coding: utf-8 -*-
from    urllib.request     import urlopen
from    bs4                import BeautifulSoup
class scrapper: 
    def get_html(self, url):
        html    = False
        headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' }
        try:
            html      = urlopen(url, '', headers).read()
        except Exception as e:
            print ("Error getting html :" + str(e))
        return html

这里是test.py

# -*- coding: utf-8 -*-
import  sys
sys.path.insert(0, "./../01_classes/class_scrapper.py")
from  class_scrapper import scrapper
o_scrapper = scrapper()

执行时,我得到:

Traceback (most recent call last):
  File "/src/00_programs/tets.py", line 6, in     <module>
    from  class_scrapper import scrapper
ImportError: No module named 'class_scrapper'

import要使命令起作用,应在命令上进行哪些更改

谢谢,

罗曼

83457

如果解释器说模块不存在,则意味着在导入时您必须将其拼写错误,或者该模块不在您程序的目录中,也不在包含所有其他主要模块的python目录中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章