0
我是Python 3的新手,正在重寫Python 2程序。我有以下文件系統:python 3 import not working
|-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
命令應該改變什麼才能使其工作?
謝謝,
羅曼。
我建議你閱讀[模塊](https://docs.python.org/3/tutorial/modules.html)。此外,它是「刮板」,請閱讀[風格指南](http://legacy.python.org/dev/peps/pep-0008/)。 – jonrsharpe 2014-08-29 10:18:25