【问题标题】:ModuleNotFoundError: No module named 'certs.check' Python ErrorModuleNotFoundError:没有名为“certs.check”的模块 Python 错误
【发布时间】:2021-10-25 04:06:03
【问题描述】:

我有一个文件夹结构

-->Certs (Folder)
   --> check.crt
--->client.py




  class Client:
        def get_parameters(self):
            client = pymongo.MongoClient(
                "mongodb://" + constants.USER_NAME + ":" + constants.PWD + constants.server +certs.check )
             print("Done")
   c=Client()
   c.get_parameters()

我收到错误“certs.check”

我试过了

from certs import *
import certs.check

请告诉我如何导入这个文件

【问题讨论】:

  • import 语句专门用于 python 文件。要打开文件并从中读取,请使用 open 方法。

标签: python python-3.x mongodb pymongo


【解决方案1】:

对于 PY 文件:

尝试以下步骤:

  1. 在certs文件夹中,添加一个名为__init__.py的空文件,并将__init__.py文件设为空。

  2. 使用以下代码导入:

    from certs import check
    

对于 IO 文件(不是 PY 文件):

尝试使用open:

with open('certs/check.crt') as f:
    # do something with `f`.

对于 MongoDB:

client = pymongo.MongoClient("mongodb://" + constants.USER_NAME + ":" + constants.PWD + constants.server + 'certs/check.crt')

【讨论】:

  • 不想打开文件想要添加为字符串这样的客户端 = pymongo.MongoClient("mongodb://" + constants.USER_NAME + ":" + constants.PWD + constants.服务器 +certs.check )
  • @user15780176 检查我的编辑,如果有效,请接受并投票
猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2019-10-28
  • 2020-02-13
  • 2020-07-30
  • 2022-01-13
  • 2020-01-01
相关资源
最近更新 更多