2016-12-08 1 views
0

私のコードに問題があります。スクレーパーとパーサーの2つのファイルがあります。それらにはほとんどインポートがありませんが、インポートエラーがまだ発生しています。名前をインポートできません<name_of_function> - 循環インポートが表示されません

from parser import parse_price

ImportError: cannot import name 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) 
+0

を、あなたはそれを適切に扱うことができるどこかに例外バブルを許可してください。また、 'return'のチャンスがある前に' raise'するので、うまくいきません。 – jonrsharpe

+0

@MilanoどのOSを使用していますか?あなたのファイル名を 'parser.py'以外のものに変更しようとしましたか? – ettanany

答えて

1

parserは、Pythonでの既存のモジュールであるので、あなたは、あなたのファイルparser.pyを呼び出すべきではありません。よりよく理解するために、以下をお読み

>>> 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を持っていません:エラー情報がPython的ではありません含まれているオブジェクトを返す

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

Pythonは標準ライブラリの前にローカルの 'parser' *を使います。 – jonrsharpe

+0

@jonrsharpeあなたはあなたのマシンでそれを試してみて、どの結果が得られているか教えてください。私はあなたが言ったことを知っていたが、私はそれが私のWindowsマシンの場合ではないことに驚いています。私は 'parser.py'と' parser_2.py'を同じフォルダに作成しました.parser_2をインポートするとうまく動作しますが、 'parser'をインポートすると、代わりにpythonモジュールがインポートされます! – ettanany

+0

私のUbuntuマシンでチェックしてもうまく動作し、期待通り 'import parser'はPythonのものではなくローカルモジュールをインポートします。 – ettanany

関連する問題