【问题标题】:no module named 'dotenv' python 3.8没有名为“dotenv”的模块 python 3.8
【发布时间】:2020-04-21 15:21:35
【问题描述】:

编辑:已解决,如果有人遇到此 python3.8 -m pip install python-dotenv 为我工作。

我已经尝试重新安装 dotenv 和 python-dotenv,但我仍然遇到同样的错误。我确实在与此脚本相同的目录中有 .env 文件。

#bot.py
import os
import discord

from dotenv import load_dotenv
load_dotenv()


token=os.getenv('DISCORD_TOKEN')

client = discord.Client()


@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')


client.run(token)

【问题讨论】:

  • 你是如何安装dotenv的?
  • 你确定你没有使用 venv 吗?
  • 你能粘贴文件夹树的样子吗?谢谢:)
  • 我在终端中安装了这样的 dotenv:pip install python-dotenv
  • 试试python3.8 -m pip install python-dotenv

标签: python import module


【解决方案1】:

就我而言,我在 zsh 控制台中将 python 别名为 python3

使用python3 -i filename.py 运行.py 文件使其工作。

【讨论】:

    【解决方案2】:

    如果您使用的是poetry (https://python-poetry.org),那么...

    确保你正在做:

    $ poetry run SOME_COMMAND
    # such as `poetry run pytest`
    

    不仅仅是:

    $ SOME_COMMAND
    # such as `pytest`
    

    我的问题(详细)是……

    # Starting with a _contrasting_ example that _unexpectedly worked_..
    
    # A long time ago I did..
    
    $ pip install pytest
    
    # And now, I was doing everything below..
    
    $ poetry add requests
    
    # ..Then I wrote code that has `import requests` in it
    # ..Then I wrote some unit test code (to use with pytest) to test the use of `requests`
    
    # ..And NOT knowing I'm supposed to do `poetry run pytest`, I was just doing
    
    $ pytest
    
    # ..And it (oddly) worked, perhaps because maybe `requests` had been installed globally somewhere for me.
    
    # ..But then I did
    
    $ poetry add python-dotenv 
    
    # ..Then I wrote code that had `from dotenv import load_dotenv` in it
    # ..Then I wrote some unit test code (to use with pytest) to test the use of `python-dotenv`
    
    # And I got the error I should have gotten..
    
    $ pytest
    
    # ..a bunch of error output, including..
    ModuleNotFoundError: No module named 'dotenv'
    

    因此,修复是:

    # Get rid of the global pytest. Don't want to use that.
    # (I think this step is optional, b/c I think `poetry run pytest` below will use the pytest installed via poetry in your virtual env (i.e. I _think_ it will (on it's own) NOT use the globally installed pytest.))
    $ pip uninstall pytest
    
    $ poetry add python-dotenv
    $ poetry add --dev pytest
    
    $ poetry run pytest
    

    修复的功劳归于:

    https://github.com/joeyespo/pytest-watch/issues/112

    【讨论】:

      【解决方案3】:

      如果是 Ubuntu 或 Debian,请在您的安装管理器中尝试: apt install python3-dotenv

      你也可以尝试sudo pip3 install python-dotenv通过pip安装。

      无论您做什么,都记得明确包含缺少的 3 部分。

      Debian/Ubuntu 有单独的软件包,目前 python 在它们的 apt 存储库中表示 python2python3 表示 python3。但是,当涉及到您在系统上本地安装的 python 二进制文件时,它默认使用的 python 二进制文件可能会有所不同,具体取决于系统上 /usr/bin/python 符号链接到的内容。有些系统符号链接到 python2.7 之类的东西,而其他系统可能是 python3.5 之类的东西。本地安装的pip 也存在类似问题。因此,为什么在安装或搜索 python 包时使用“3”很重要

      【讨论】:

      • 试过 pip3 install python-dotenvpython3-dotenv 但我仍然遇到同样的错误。你在我的安装管理器中是什么意思(我在 ubuntu 上)?
      • 使用第一个选项安装正确的包--->apt install python3-dotenv
      【解决方案4】:

      这将解决仅通过终端安装的问题:

      pip3 install python-dotenvfor python 3.0 版本或pip install python-dotenv for python 3.0 以外的版本

      【讨论】:

        【解决方案5】:

        我是 Python 新手,遇到了完全相同的错误。刚刚将安装命令更改为我在 MongoDB 教程中使用的安装 PyMongo 的命令。它就像一个魅力:)

        python -m pip install python-dotenv

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-06
          • 2021-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-30
          • 1970-01-01
          相关资源
          最近更新 更多