如何导入Redis Python模块?

2024-04-26 12:32:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我运行项目时收到以下错误:

File "/home/nguyentv/schoollink/web/views/apis.py", line 10, in <module>
    from util.redis.redis_client import Redis
ImportError: No module named util.redis.redis_client

如何正确导入此库?你知道吗


Tags: 项目pyredisclientwebhome错误util
2条回答

您正试图从名为util的包导入Redis。除非此包是应用程序的一部分,否则它不存在。你知道吗

根据python-redis' documentation,下面是如何导入它:

import redis
# then use redis.Redis(...)

或者,相当于:

from redis import Redis
# then use Redis(...)

The Module Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory). PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

基本上,解释器将在当前工作目录中执行查找,然后它将搜索系统定义的库目录。你知道吗

您面临的问题可能是代码正在查找不存在的模块、从错误的目录调用脚本,或者sys.path的设置不正确。你知道吗

如果您演示如何实例化解释器,pwd输出和tree输出,我可以提供更多帮助。你知道吗

相关问题 更多 >