简体   繁体   English

没有名为“dotenv”的模块 python 3.8

[英]no module named 'dotenv' python 3.8

EDIT: Solved, if anyone comes across this python3.8 -m pip install python-dotenv worked for me.编辑:解决了,如果有人遇到这个python3.8 -m pip install python-dotenv为我工作。

I've tried reinstalling both dotenv and python-dotenv but I'm still getting the same error.我试过重新安装 dotenv 和 python-dotenv 但我仍然遇到同样的错误。 I do have the.env file in the same directory as this script.我在与此脚本相同的目录中确实有 .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)

I'm new to Python and just got exact same error.我是 Python 新手,遇到了完全相同的错误。 Just changed install command to what I used from a MongoDB tutorial to install PyMongo.刚刚将安装命令更改为我在 MongoDB 教程中使用的安装 PyMongo 的命令。 It worked like a charm :)它就像一个魅力:)

python -m pip install python-dotenv

in your installation manager if it's Ubuntu or Debian try: apt install python3-dotenv如果是 Ubuntu 或 Debian,请在安装管理器中尝试: apt install python3-dotenv

you can also try sudo pip3 install dotenv to install via pip.您也可以尝试sudo pip3 install dotenv通过 pip 安装。

Whatever you do remember to include explicitly the missing 3 part.无论您确实记得明确包括缺少的3部分。

Debian/Ubuntu have separate packages and as of the present time python means python2 and python3 means python3 in their apt repositories. Debian/Ubuntu 有单独的软件包,目前python意味着python2python3意味着python3在它们的 apt 存储库中。 However, when it comes to your locally installed python binary on your system which python binary it defaults to using may vary depending on what /usr/bin/python is symlinked to on your system.但是,当涉及到系统上本地安装的python二进制文件时,它默认使用的 python 二进制文件可能会有所不同,具体取决于 /usr/bin/python 在系统上符号链接到的内容。 Some systems it's symlinked to something like python2.7 and other's it may be something like python3.5 .有些系统符号链接到类似python2.7的东西,而其他系统可能是类似python3.5的东西。 Similar issues exist with locally installed pip .本地安装的pip也存在类似问题。 Hence, why using the '3' is important when installing or searching for python packages因此,为什么在安装或搜索 python 包时使用“3”很重要

This will solve just installing via terminal:这将解决仅通过终端安装的问题:

pip3 install python-dotenv for python 3.0 versions or pip install python-dotenv for python different than 3.0 pip3 install python-dotenv for python 3.0 版本或pip install python-dotenv for python 3.0 以外的版本

If you're using poetry ( https://python-poetry.org ), then...如果您使用的是poetryhttps://python-poetry.org ),那么...

Be sure you're doing:确保你正在做:

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

Instead of just:而不仅仅是:

$ SOME_COMMAND
# such as `pytest`

My problem (in detail) was...我的问题(详细)是......

# 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'

Thus, the fix was:因此,修复是:

# 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

Credit for the fix goes to:修复的功劳归于:

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

In my case, I was aliasing python to python3 , in my zsh console.就我而言,我在我的 zsh 控制台中将python别名为python3

Running a .py file with python3 -i filename.py made it work.使用python3 -i filename.py运行.py文件使其工作。

I had the same issue because I was running the installation command locally (in my virtual env).我遇到了同样的问题,因为我在本地(在我的虚拟环境中)运行安装命令。

When running it globally (outside of virtual env) it finally resolved the issue ;)在全局运行它时(在虚拟环境之外),它终于解决了这个问题;)

the below line code to execute in terminal will solve the issue.在终端中执行的以下行代码将解决该问题。 pip3 install python-dotenv pip3 安装 python-dotenv

I had the same issue (Python 3.8.5, dotenv 0.15.0) and was getting ModuleNotFoundError: No module named 'dotenv' in both the console and JupyterLab.我遇到了同样的问题(Python 3.8.5,dotenv 0.15.0)并且在控制台和 JupyterLab 中都遇到了 ModuleNotFoundError: No module named 'dotenv'。 All other packages seemed to install via pip with no problems.所有其他软件包似乎都通过 pip 安装,没有任何问题。

I just ran:我刚跑:

pip3 uninstall python-dotenv pip3 卸载 python-dotenv

pip3 install -U python-dotenv pip3 安装 -U python-dotenv

In my case got this error while running pytest .就我而言,运行pytest时出现此错误。
Issue got resolved by calling python3 -m pytest <- inside poetry通过调用python3 -m pytest <- 在诗歌中解决了问题

I had the same problem, and tried everything here, but once I closed out of my terminal windows and reopened them, the library was finally recognized.我遇到了同样的问题,并尝试了这里的所有方法,但是一旦我关闭终端 windows 并重新打开它们,图书馆终于被识别出来了。

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

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