【问题标题】:python 3 import not workingpython 3导入不起作用
【发布时间】:2014-08-29 10:16:05
【问题描述】:

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

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

我想从文件class_scrapper中导入类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 命令进行哪些更改才能使其正常工作?

谢谢,

罗曼。

【问题讨论】:

标签: python-3.x import


【解决方案1】:

如果解释器说模块不存在,这意味着您在导入时一定拼错了,或者该模块不在您的程序目录或包含所有其他主要模块的 python 目录中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2015-07-27
    • 2018-01-08
    • 2014-10-31
    • 2018-07-10
    相关资源
    最近更新 更多