编辑:解决了,如果有人遇到这个python3.8 -m pip install python-dotenv
为我工作。
我已经尝试重新安装 dotenv 和 python-dotenv,但我仍然收到同样的错误。
#bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
token=os.getenv('DISCORD_TOKEN')
client = discord.()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(token)

我是 Python 的新手,只是得到了完全相同的错误。只是将 install 命令更改为我从 MongoDB 教程中用来安装 PyMongo 的。它的工作就像一个魅力:)
python -m pip install python-dotenv

如果是 Ubuntu 或 Debian,请在安装管理器中尝试:apt install python3-dotenv
您也可以尝试sudo pip3 install dotenv
通过 pip 安装。
无论您做什么,都要记住明确包含缺少的3部分。
因此,类似的 Debian / Ubuntu 有单独的软件包,并且目前python
意味着python2
和python3
意味着python3
在其 apt 存储库中。但是,当涉及到您的系统上本地安装的python
二进制文件时,它默认使用的/ bin / symthon

这将解决只是通过终端安装:
pip3 install python-dotenv
适用于 python 3.0 版本或pip install python-dotenv
适用于不同于 3.0 的 python
如果您使用的是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本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(59条)