无法导入名称<name_of_function> - 无法查看循环导入

时间:2016-12-08 09:43:29

标签: python python-2.7 python-import circular-dependency

我的代码有问题。有两个文件 - 刮刀和解析器。其中的进口很少,但我仍然遇到导入错误。

  

来自解析器导入parse_price

     

ImportError:无法导入名称parse_price

我认为这应该是由循环进口引起的,但我找不到......

我通过pycharm运行scraper.py。 (有一行print get_price(...

你能看到问题吗?

parser.py

# -*- coding: utf-8 -*-

"""
Parse prices strings in various formats

e.g: $1,997.00

"""
import decimal
from exceptions import Exception
import re

_CURRENCIES = u'eur;czk;sk;kč'.split(';')
_SIGNS = u'$;€;£'.split(';')

CURRENCY_STRINGS = [x for x in _CURRENCIES]+[x.capitalize() for x in _CURRENCIES]+[x.upper() for x in _CURRENCIES]
CURRENCY_SIGNS = _SIGNS

REMOVE_STRINGS = CURRENCY_STRINGS + CURRENCY_SIGNS


class PriceParseException(Exception):
    pass

def parse_price(text):
    text = text.strip()
    # try:
    non_decimal = re.compile(r'[^\d,.]+')
    text = non_decimal.sub('', text).replace(',','.')
    if not len([x for x in text if x in '123456789']):
        raise Exception
    price = decimal.Decimal(text.strip())
    # except:
    #     raise
    #     raise PriceParseException()

    return price



from tld import get_tld,update_tld_names #TODO: from tld.utils import update_tld_names; update_tld_names() + TABULKU/MODEL TLD_NAMES

def parse_site(url):
    try:
        return get_tld(url)
    except:
        return None

scraper.py:

import requests
from django.utils.datetime_safe import datetime
from lxml import etree
from exceptions import Exception

class ScanFailedException(Exception):
    pass

def _load_root(url):
    r = requests.get(url)
    r.encoding = 'utf-8'
    html = r.content
    return etree.fromstring(html, etree.HTMLParser())

def _get_price(url,xpath):
    root = _load_root(url)
    try:
        return root.xpath(xpath+'/text()')[0]
    except:
        raise ScanFailedException()

def get_price(url,xpath):
    response = {}
    root = _load_root(url)
    try:
        price = ''.join([x for x in root.xpath(xpath+'/text()') if any(y in x for y in '123456789')])
    except:
        response['error'] = 'ScanFailedException'
        return response

    try:
        from parser import parse_price #HERE IS THE PROBLEM
        decimal_price = parse_price(price)
        response['price']=str(decimal_price)
    except:
        raise
        response['error'] = 'PriceParseException'
        return response
    return response



print get_price('some_url',
          '//strong[@class="product-detail-price"]')

def scrape_product(product):
    from main_app.models import Scan
    for occ in product.occurences.all():
        try:
            from parser import parse_price
            url = occ.url
            xpath = occ.xpath
            raw_price = _get_price(url,xpath)

            price = parse_price(raw_price)
            Scan.objects.create(occurence=occ, datetime=datetime.now(), price=price)
        except:
            """Create invalid scan"""
            Scan.objects.create(occurence=occ,datetime=datetime.now(),price=None,valid=False)

1 个答案:

答案 0 :(得分:1)

您不应该调用文件parser.py,因为parser是Python中的现有模块。

通读以下内容以便更好地理解:

>>> import parser
>>> 
>>> dir(parser)
['ASTType', 'ParserError', 'STType', '__copyright__', '__doc__', '__name__', '__package__', '__version__', '_pickler', 'ast2list', 'ast2tuple', 'compileast', 'compilest', 'expr', 'isexpr', 'issuite', 'sequence2ast', 'sequence2st', 'st2list', 'st2tuple', 'suite', 'tuple2ast', 'tuple2st']
>>> 
>>> 'parse_price' in dir(parser)
False

你看,Python parser模块没有parse_price,然后是:

>>> from parser import parse_price
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name parse_price