2

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.

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'
SecretLloyd
  • 109
  • 1
  • 13

2 Answers2

2

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.

How to use the virtual environment?

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

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

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

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:

  • Open VSCode in your project
  • Press F1
  • Type: > python: select interpreter
  • Click on Enter path or find an existing interpreter
  • Click on Find
  • The navigate to .env > Scripts > python
Felipe Endlich
  • 540
  • 5
  • 24
0

3 possible cases:

  1. The same thing happened to me when I did not notice I was using two Pythons at the same time one 2.7 and another one 3.6. Make sure to know where is your package being installed to the Python modules folder you really want to store it or in another one you did not know existed.

  2. Your PATH might not be configured correctly, check out either if you are using Windows or Linux if your PATH variables are configured correctly. You can reset your configuration if you wish. (link= How to reload .bashrc settings without logging out and back in again?)

  3. For some packages/libraries of Python the way of importing the library is different from the name you import it on your .py file. For example: You can install OpenCV library by [pip install OpenCV] but when importing it in a file you have to write [import cv2].

I hope you find this information helpful for your problem.