Python 3.x Python3导入不工作
我是Python3新手,正在重写Python2程序。我有以下文件系统:Python 3.x Python3导入不工作,python-3.x,import,Python 3.x,Import,我是Python3新手,正在重写Python2程序。我有以下文件系统: |-00_programs / test.py |-01_classes / class_scrapper.py 我想从文件class\u scraster导入classscraster: 这是class_scraster.py: # -*- coding: utf-8 -*- from urllib.request import urlopen from bs4 impo
|-00_programs / test.py
|-01_classes / class_scrapper.py
我想从文件class\u scraster
导入classscraster
:
这是class_scraster.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
# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "./../01_classes/class_scrapper.py")
from class_scrapper import scrapper
o_scrapper = scrapper()
下面是test.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
# -*- 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'
回溯(最近一次呼叫最后一次):
文件“/src/00_programs/tets.py”,第6行,在
从U类刮板机进口刮板机
ImportError:没有名为“class_scraster”的模块
在import
命令上应该更改哪些内容才能使其正常工作
谢谢
Romain。如果解释器说模块不存在,这意味着您在导入时一定拼错了,或者模块不在您的程序目录或包含所有其他主要模块的python目录中。我建议您继续阅读。还有,这是“刮刀”,请阅读。