出于某种奇怪的原因,无论我在导入时安装哪个包,它都不知道我在说什么包。我非常确定这是 Visual Studio Code 错误,但如果不是,我也在使用 Linux。
当我pip install
包pyttsx3
时,这是我在终端中得到的:
Collecting pyttsx3
Downloading https://files.pythonhosted.org/packages/24/4e/580726c73272344d3e74b7aaffae55ff6b6450061fbecb8cc6e112531c02/pyttsx3-2.7.tar.gz
Building wheels for collected packages: pyttsx3
Running setup.py bdist_wheel for pyttsx3 ... done
Stored in directory: /home/secretlloyd/.cache/pip/wheels/a2/8a/fe/11112aca9c89142c3a404bc67ef3393a7ad530da26639a05d4
Successfully built pyttsx3
Installing collected packages: pyttsx3
Successfully installed pyttsx3-2.7
但是当我运行示例时,我收到此错误:
Traceback (most recent call last):
File "/home/secretlloyd/Visual Studio Code/Python/Finished/Text Colors/finished.py", line 1, in <module>
import pyttsx3
ModuleNotFoundError: No module named 'pyttsx3'
最佳答案
您可以使用虚拟环境来安装库。如果这样做,每个项目都将拥有自己的作用域库,而不会影响您的全局库。
如何使用虚拟环境?
输入项目的根文件夹,然后在 bash 上运行以下命令:
$ py -m venv .env
$ source .env/Scripts/activate
之后您会注意到您的 bash 将具有类似 (.env)
的前缀。然后你应该安装你的库:
(.env) $ pip install pyttsx3
要停用虚拟环境,只需运行以下命令:
(.env) $ deactivate
为虚拟环境设置 VS Code Intellisense
如果您使用 VSCode,您可以在设置虚拟环境后设置正确的 python 解释器。只需按照以下步骤操作即可:
- 在项目中打开 VSCode
- 按 F1
- 类型:
> python:选择解释器
- 点击
输入路径或查找现有解释器
- 点击
查找
- 导航到
.env > Scripts > python
关于python - 已安装包但未导入包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68433817/