简体   繁体   English

如何导入Redis Python模块?

[英]How Do I Import the Redis Python Module?

I ran my project and received the following error: 我运行了我的项目并收到以下错误:

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

How do I properly import this library? 如何正确导入此库?

The Module Search Path 模块搜索路径

When a module named spam is imported, the interpreter first searches for a built-in module with that name. 导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。 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给出的目录列表中搜索名为spam.py的文件。 sys.path is initialized from these locations: sys.path从这些位置初始化:

  • 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). PYTHONPATH(目录名列表,语法与shell变量PATH相同)。
  • the installation-dependent default. 依赖于安装的默认值。

After initialization, Python programs can modify sys.path. 初始化后,Python程序可以修改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. 有关更多信息,请参见标准模块一节

Basically, the interpreter is going to perform a lookup in your current working directory, then it will search through the system defined library directories. 基本上,解释器将在您当前的工作目录中执行查找,然后它将搜索系统定义的库目录。

The issue you are facing could be that your code is looking for a module that does not exist, you are calling the script from an incorrect directory, or the sys.path is setup incorrectly. 您面临的问题可能是您的代码正在查找不存在的模块,您从不正确的目录调用脚本,或者sys.path设置不正确。

I could help more if you showed how you instantiated the interpreter, pwd output, and tree output. 如果你展示了如何实例化解释器, pwd输出和tree输出,我可以提供更多帮助。

You are trying to import Redis from a package named util . 您正在尝试从名为util的包导入Redis。 Unless this package is part of your application, it does not exist. 除非此软件包是您的应用程序的一部分,否则它不存在。

According to python-redis' documentation , here is how to import it: 根据python-redis的文档 ,这里是如何导入它:

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

or, equivalently: 或者,等效地:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM