question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

python manage.py oscar_populate_countries throws error -

See original GitHub issue

Hello,

while running the following command I get this error -

./manage.py oscar_populate_countries

or

python manage.py oscar_populate_countries -

This’ the log.

./manage.py oscar_populate_countries
/Users/akos/oscar/lib/python2.7/site-packages/django/utils/six.py:808: RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead.
  return meta(name, bases, d)

/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py:345: RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead
  self.fetch_command(subcommand).run_from_argv(self.argv)

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/akos/oscar/lib/python2.7/site-packages/oscar/management/commands/oscar_populate_countries.py", line 58, in handle
    for country in pycountry.countries]
  File "/Users/akos/oscar/lib/python2.7/site-packages/pycountry/db.py", line 22, in __getattr__
    raise AttributeError
AttributeError


I have been google to find the answer. And evening ended up creating a whole new project again. What Im I doing wrong?

Regards

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
albinantony2904commented, Jan 5, 2017

As pycountry made new changes, the command oscar_populate_countries won’t work.So for an alternate way for populating countries, please follow steps below.

-create a new file say populate_countries.py with the following code:-

# -*- coding: utf-8 -*-
import sys
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from oscar.core.loading import get_model

Country = get_model('address', 'Country')


class Command(BaseCommand):
    help = "Populates the list of countries with data from pycountry."


    option_list = BaseCommand.option_list + (
        make_option(
            '--no-shipping',
            action='store_false',
            dest='is_shipping',
            default=True,
            help="Don't mark countries for shipping"),
        make_option(
            '--initial-only',
            action='store_true',
            dest='is_initial_only',
            default=False,
            help="Exit quietly without doing anything if countries were already populated."),
    )

    def handle(self, *args, **options):
        try:
            import pycountry
        except ImportError:
            raise CommandError(
                "You are missing the pycountry library. Install it with "
                "'pip install pycountry'")

        if Country.objects.exists():
            if options.get('is_initial_only', False):
                # exit quietly, as the initial load already seems to have happened.
                self.stdout.write("Countries already populated; nothing to be done.")
                sys.exit(0)
            else:
                raise CommandError(
                    "You already have countries in your database. This command "
                    "currently does not support updating existing countries.")

        countries = [
            Country(
                iso_3166_1_a2=country.alpha_2,
                iso_3166_1_a3=country.alpha_3,
                iso_3166_1_numeric=country.numeric,
                printable_name=country.name,
                name=getattr(country, 'official_name', ''),
                is_shipping_country=options['is_shipping'])
            for country in pycountry.countries]

        Country.objects.bulk_create(countries)
        self.stdout.write("Successfully added %s countries." % len(countries))


-move this file to your_apps_folder/management/commands. Make sure that you add __init__.py to management and commands folder.Refer this for help.

-after completing all the above steps, run command as python manage.py populate_country.This command will populate db with countries

1reaction
adlhcommented, Dec 18, 2016

Same for me on a 100% fresh installation.

These are my installed packages in the venv:

$ pip freeze
Babel==2.3.4
Django==1.9
django-extra-views==0.6.4
django-haystack==2.5.1
django-oscar==1.3
django-tables2==1.0.7
django-treebeard==4.1.0
django-widget-tweaks==1.4.1
factory-boy==2.8.1
Faker==0.7.5
mock==2.0.0
pbr==1.10.0
phonenumbers==7.7.5
Pillow==3.4.2
purl==1.3
pycountry==16.11.27.1
python-dateutil==2.6.0
pytz==2016.10
six==1.10.0
sorl-thumbnail==12.4a1
Unidecode==0.4.19

UPDATE:

I reinstalled pycountry with the version specified on the requirements.txt (on the github repos), which is 1.8, and then everything ran without any errors. So maybe the preferred installation method is to checkout the github repos first, and then pip install from requirements.txt?

Read more comments on GitHub >

github_iconTop Results From Across the Web

python manage.py runserver throwing error - django
I am getting the error given below. I am not able to start server. Please guide me? C:\training\webserver\webserver>python manage.py runserver ...
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
The most common pattern for handling Exception is to print or log the exception and then re-raise it (allowing a caller to handle...
Read more >
Python Exceptions: An Introduction - Real Python
In this beginner tutorial you'll learn what exceptions are good for in Python. You'll see how to raise exceptions and how to handle...
Read more >
How to Throw Exceptions in Python - Rollbar
Difference Between Python Syntax Errors and Python Exceptions ... File "test.py", line 13, in raise Exception("Date provided can't be in the ...
Read more >
Django settings - Django documentation
The command python manage.py diffsettings displays differences between the ... will raise an ImportError exception the first time a setting is accessed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found