首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImportError引发试图导入函数

ImportError引发试图导入函数
EN

Stack Overflow用户
提问于 2017-10-30 14:50:14
回答 2查看 420关注 0票数 0

上下文:

我正在尝试构建一个Django应用程序,它使用这个包:django-minio-存储。我试图用以下类扩展包中的某个类:

代码语言:javascript
复制
@deconstructible
class MinioStoreStorage(MinioStorage):
    def __init__(self, bucket_name):
        client = create_minio_client_from_settings()
        base_url = bucket_name
        # base_url = get_setting("MINIO_STORAGE_STATIC_URL", None)
        bucket_name = bucket_name
        auto_create_bucket = True
        presign_urls = True

        super(MinioStoreStorage, self).__init__(
            client,
            bucket_name,
            auto_create_bucket=auto_create_bucket,
            base_url=base_url,
            presign_urls=presign_urls
        )

问题:

我不能导入函数create_minio_client_from_settings。此函数驻留在包的文件storage.py中。驻留在类MinioStorage中的同一个文件。我还可以成功地导入另一个函数(get_setting),它位于同一个文件中,没有问题地使用它,但是尝试为create_minio_client_from_settings做同样的操作会引发一个ImportError。以下是我使用的导入:

代码语言:javascript
复制
from minio_storage.storage import get_setting
# Succeeds
from minio_storage.storage import create_minio_client_from_settings
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'create_minio_client_from_settings'

storage.py

下面是包的代码片段:

代码语言:javascript
复制
@deconstructible
class MinioStorage(Storage):
    """An implementation of Django's file storage using the minio client.

    The implementation should comply with
    https://docs.djangoproject.com/en/dev/ref/files/storage/.

    """
    ...
    ...
    ...

def get_setting(name, default=_NoValue, ):
    result = getattr(settings, name, default)
    if result is _NoValue:
        print("Attr {} : {}".format(name, getattr(settings, name,                     default)))
        raise ImproperlyConfigured
    else:
        return result


def create_minio_client_from_settings():
    endpoint = get_setting("MINIO_STORAGE_ENDPOINT")
    access_key = get_setting("MINIO_STORAGE_ACCESS_KEY")
    secret_key = get_setting("MINIO_STORAGE_SECRET_KEY")
    secure = get_setting("MINIO_STORAGE_USE_HTTPS", True)
    client = minio.Minio(endpoint,
                         access_key=access_key,
                         secret_key=secret_key,
                         secure=secure)
    return client

问题:

  • 如何导入create_minio_client_from_settings函数?
  • 有办法解决这个问题吗?(我知道我可以将它作为一个辅助函数直接复制到我的项目中)
  • 不能够从包导入函数的原因是什么,而对于同一个文件中的函数,我可以这样做吗?

进一步调查:

我一直在更多地研究这个问题,这里有一些评论和一种观察异常现象的更可重复的方法。在创建了Django项目并安装了有问题的包之后,我使用:python manage.py shell启动了shell,并使用了以下命令:

代码语言:javascript
复制
>>> import minio_storage
>>> dir(minio_storage)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__',         '__name__', '__package__', '__path__', '__spec__', '__version__',     'storage']
>>> help(minio_storage.storage)

help(minio_storage.storage)将显示一个描述包提供的类和函数的手册页面。在函数类别下,只有一个函数可用,它就是get_setting()函数。

  • 为什么create_minio_client_from_settings()不像get_setting()那样出现呢?
  • 包开发人员有可能隐藏他的一些功能吗?

版本和依赖项

下面是命令的结果:pipenv graph

代码语言:javascript
复制
django-minio-storage==0.1.0
  - django [required: >=1.9, installed: 1.11.6]
    - pytz [required: Any, installed: 2017.2]
  - minio [required: >=1.0.2, installed: 2.2.5]
    - certifi [required: Any, installed: 2017.7.27.1]
    - pytz [required: Any, installed: 2017.2]
    - urllib3 [required: Any, installed: 1.22]
djangorestframework==3.7.1
flake8==3.5.0
  - mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1]
  - pycodestyle [required: >=2.0.0,<2.4.0, installed: 2.3.1]
  - pyflakes [required: >=1.5.0,<1.7.0, installed: 1.6.0]
Pillow==4.3.0
  - olefile [required: Any, installed: 0.44]
EN

回答 2

Stack Overflow用户

发布于 2017-10-31 13:38:28

您的django-minio-storage包已经过时,但这并不是您的错--在撰写本文时,PyPi包本身已经过时了。

您正在运行的版本不包含您要寻找的函数。从GitHub下载包的源代码,并在那里运行-您将得到您想要的行为:https://github.com/py-pa/django-minio-storage

票数 2
EN

Stack Overflow用户

发布于 2017-10-31 18:04:24

解决了

正如用户souldeux所指出的,我的包裹过时了。我使用pipenv安装它,它不是一个与最新版本匹配的版本。我在一边做着我过时的包,另一边写着来自github的新代码。

他提供的链接很有用,但我结束了使用另一个所以回答直接从github安装软件包。更具体地说,就我而言:

代码语言:javascript
复制
pipenv install git+https://github.com/py-pa/django-minio-storage.git#egg=django-minio-storage
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47018200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档