Scrapy - NameError: global name 'base_search_url' is not defined
<p>I am trying to call a local variable from inside a Scrapy spider class but then I got <code>NameError: global name 'base_search_url' is not defined</code>.</p>

<pre><code>class MySpider(scrapy.Spider):
    name = "mine"
    allowed_domains = ["www.example.com"]

    base_url = "https://www.example.com"

    start_date = "2011-01-01"

    today = datetime.date.today().strftime("%Y-%m-%d")

    base_search_url = 'https://www.example.com/?city={}&amp;startDate={}&amp;endDate={}&amp;page=1',

    city_codes = ['on', 'bc', 'ab']

    start_urls = (base_search_url.format(city_code, start_date, today) for city_code in city_codes)
</code></pre>

<p>I tried to use <code>self.base_search_url</code> instead but there is no use. Does anyone know how to solve it?</p>

<p>FYI, I use Python 2.7</p>

<p>Solved! I end up solving it by using <code>__init__()</code> function.</p>

<pre><code>def __init__(self):
    self.start_urls = (self.base_search_url.format(city_code, self.start_date, self.today) for city_code in self.city_codes)
</code></pre>

python python-2.7 scrapy scrapy-spider local-variables