语境:
我正在尝试构建一个使用此包的 Django 应用程序:django-minio-storage。我正在尝试使用以下类扩展包中的某个类:
@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
. 这是我正在使用的导入:
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'
存储.py
这是包的代码片段:
@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 项目并安装有问题的包后,我使用 : 启动了 shell,python manage.py shell
并使用了以下命令:
>>> 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
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]