已安装包但未导入包 [英] Package installed but doesn't import package

查看:56
本文介绍了已安装包但未导入包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种奇怪的原因,无论我在转到 import 时安装哪个包,它都不知道我在说什么包.我非常确定这是一个 Visual Studio Code 错误,但如果不是,我也在使用 Linux.

For some odd reason no matter which package I install when I go to import it doesn't know what package I'm talking about. I am very certain this is a Visual Studio Code error but if not I am also using Linux.

当我 pip installpyttsx3 这就是我在终端中得到的:

When I pip install the package pyttsx3 this is what I get in the Terminal:

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

但是当我运行一个例子时,我得到这个错误:

But when I run a example I get this error:

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'

推荐答案

您可以使用虚拟环境来安装库.如果这样做,每个项目都将拥有自己的作用域库,而不会影响您的全局库.

You can use an virtual environment to install your libs. If you do that, each project will have its own scoped libs without affect your global libs.

进入你项目的根文件夹,然后在 bash 上运行以下命令:

Enter the root folder of your project and then run the following commands on the bash:

$ py -m venv .env
$ source .env/Scripts/activate

之后你会注意到你的 bash 会有一个类似 (.env) 的前缀.然后你应该安装你的库:

After that you'll notice your bash will have a prefix like that (.env). Then you should install your libs:

(.env) $ pip install pyttsx3

要停用虚拟环境,只需运行以下命令:

In order to deactivate the virtual environment just run the following command:

(.env) $ deactivate

为虚拟环境设置 VS Code Intellisense

如果您使用的是 VSCode,则可以在设置虚拟环境后设置正确的 Python 解释器.只需按照以下步骤操作:

Setup VS Code Intellisense for Virtual Environment

If you're using VSCode you can set the correct python interpreter after setting up a virtual environment. Just follow the steps:

  • 在您的项目中打开 VSCode
  • 按 F1
  • 类型:<代码>>python:选择解释器
  • 点击Enter path or find an existing interpreter
  • 点击查找
  • 导航到 .env >脚本 >蟒蛇

这篇关于已安装包但未导入包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆