سؤال

I'm working on a django project which uses a package called django-google-storage.

I installed the package into a virtualenv using

pip install django-google-storage

Now I want to reference the package from within my app -

from django-google-storage.storage import GoogleStorage

But of course I can't because there's hyphens in the name (invalid syntax).

I've never come across this before - I might install a package with hyphens in the package name (eg pip install django-grappelli), but the package is always saved to the virtualenv with a valid identifier in the name (eg 'grappelli').

What should I do to get this working?

هل كانت مفيدة؟

المحلول

import importlib
themodule = importlib.import_module('django-google-storage')

But, according to the readme on github:

It' [sic] just a compilation of django-storages and boto to improve you abilities to use Google Storage.

I don't think the author intended for you to import this module.

نصائح أخرى

This module is not intended to be imported directly. It is intended to override the django storage class.

django/core/storage.py

class Storage(object):
    """
    A base storage class, providing some default behaviors that all other
    storage systems can inherit or override, as necessary.
    """

So follow these instructions you need to:

Modify your settings.py and:

  1. add 'django-google-storage' to your INSTALLED_APPS
  2. put 'django-google-storage.storage.GoogleStorage' in DEFAULT_FILE_STORAGE.
  3. add GS_ACCESS_KEY_ID, GS_SECRET_ACCESS_KEY and GS_STORAGE_BUCKET_NAME

That should be it.

You should not import this module. Set the 'django-google-storage.storage.GoogleStorage' class as your DEFAULT_FILE_STORAGE and you're done

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top