DEV Community

Cover image for Understanding Why I Couldn't Find Installed Packages
Naufan Rusyda Faikar
Naufan Rusyda Faikar

Posted on • Edited on

Understanding Why I Couldn't Find Installed Packages

For you guys, as always, I would recommend to completing the official tutorial. Try not to end up impatiently learning something. "Reading is a window to the world!"—Lynn Butler

Today, we are going to share with the world some insights on how to properly install packages. Despite using Python here, we will probably find those ideas useful for other language as well. This article is written carefully for we have been asked such a question many times from the beginners. The problem usually occurs if they have just followed tutorials from different sources on the internet, but the instructors did not go into any detail about what is going on.

Multiple Installations

She came up to me with grinning and said, "I have already installed the package. So, why does it always disobey me with no module named blablabla?" The story has begun inadvertently;

> python
...
>>> from gtts import gTTS
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gtts'
Enter fullscreen mode Exit fullscreen mode

"Show me how you installed—," asked me.

"I believe I managed to install it a while ago," she continued;

> pip install gTTS
Collecting gTTS
...
Installing collected packages: urllib3, idna, chardet, certifi, requests, click, gTTS
Successfully installed certifi-2020.12.5 chardet-4.0.0 click-7.1.2 gTTS-2.2.1 idna-2.10 requests-2.25.1 urllib3-1.26.2

> pip list
Package    Version
---------- ---------
appdirs    1.4.4
certifi    2020.12.5
chardet    4.0.0
click      7.1.2
distlib    0.3.1
filelock   3.0.12
gTTS       2.2.1
idna       2.10
pip        20.3.3
requests   2.25.1
setuptools 49.2.1
six        1.15.0
urllib3    1.26.2
virtualenv 20.1.0
Enter fullscreen mode Exit fullscreen mode

"There are no other possibilities besides the fact that you have multiple installations of Python interpreter. The safest way to install packages is to use python -m pip install gTTS any time!"

"Oh my God, you are right! It is working now! But how can that be?"

"I honestly meant to explain further."

We possibly have a number of different interpreter at one time, it could be either from different sources or as different versions. You know, as we can have it installed from:

  • the official installer (from python.org);
  • the Microsoft Store (not recommended);
  • the Anaconda (if preferred);
  • the Visual Studio Installer (not usual);
  • compiled by ourselves (not needed);
  • copy pasted all files from somewhere;
  • embedded in a software (like Blender did);
  • brought in an operating system distribution (as did Fedora);
  • linked from anywhere (in the case of using virtualisation, e.g. Docker and WSL2); and
  • etc.

On Windows, we probably have:

> where.exe python
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe
C:\Users\Naufan Rusyda Faikar\AppData\Local\Programs\Python\Python39\python.exe

> where.exe pip
C:\Users\Naufan Rusyda Faikar\AppData\Local\Programs\Python\Python39\Scripts\pip.exe
C:\Users\Naufan Rusyda Faikar\AppData\Roaming\Python\Python37\Scripts\pip.exe
Enter fullscreen mode Exit fullscreen mode

The commands above tell us that we have two Python installed:

  • Python 3.7 from the official installer; and
  • Python 3.9 from the Microsoft Visual Studio Installer.

In fact, we have one more from Anaconda, but I decided not to have it included in the system path.

The where.exe python command simply searches for all python executables within the specified environment variables and then prints the list in descending order—the highest priority of python.exe to be called occupies the first place—. In Powershell, we might just want to see which executable was called-by typing its name;

> Get-Command python | Select-Object -ExpandProperty Definition
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe

> Get-Command pip | Select-Object -ExpandProperty Definition
C:\Users\Naufan Rusyda Faikar\AppData\Local\Programs\Python\Python39\Scripts\pip.exe
Enter fullscreen mode Exit fullscreen mode

Of there, we have realised that the python.exe (is Python 3.7) and pip.exe (from Python 3.9) above do not belong to each other.

Alt Text

To correct this inconsistency is to rearrange the priority order of the paths. For example changing from:

  1. Python 3.7;
  2. Python 3.9;
  3. Scripts from Python 3.9;
  4. Scripts from Python 3.7.

to:

  1. Python 3.7;
  2. Scripts from Python 3.7;
  3. Python 3.9;
  4. Scripts from Python 3.9.

where the pip.exe is included in the scripts directory. If we tend to use Python 3.9 over 3.7, just put the 3.9 and its scripts at the beginning. Do not forget to reopen the Command Prompt or Powershell once the edits have been applied.

On Linux, we can run whereis python and which python instead;

$ whereis python3
python3: /usr/bin/python3.8 /usr/bin/python3.9 /usr/bin/python3.8-config /usr/bin/python3.9-config /usr/bin/python /usr/bin/python3.8-x86_64-config /usr/bin/python3.9-x86_64-config /usr/lib/python3.8 /usr/lib/python3.9 /usr/lib64/python3.8 /usr/lib64/python3.9 /usr/local/lib/python3.8 /usr/include/python3.8 /usr/include/python3.9

$ which python3
/usr/bin/python3

$ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Dec  8 22:00 /usr/bin/python3 -> python3.9

$ pip --version
pip 20.3.1 from /home/naru/.local/lib/python3.9/site-packages/pip (python 3.9)
Enter fullscreen mode Exit fullscreen mode

It is recommended to include the version in the command whenever installing packages: python3.9 -m pip install gTTS.

In spite of having multiple installations, if we are IPython or Jupyter Notebook users, we may prefer to install packages directly by running !pip install gTTS in there. That special command is intended to permanently install the package. So, no need to run it again.

> ipython
...

In [1]: import gtts
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-d9e65c664aca> in <module>
----> 1 import gtts

ModuleNotFoundError: No module named 'gtts'

In [2]: !pip install gTTS
...

In [3]: import gtts

In [4]: gtts.__version__
Out[4]: '2.2.1'
Enter fullscreen mode Exit fullscreen mode

Virtual Environments

On another day, she has come back and complained, "Why on earth the exact error came back?"

Alt Text

After she sent me a screenshot of all the things she could give, I smile with amusement.

"Are you insulting me?"

"No way! You are using PyCharm, are not you? Have you paid attention to the dialog that appears when creating a new project?"

Alt Text

"Do not make me feel guilty!"

"Of course you must! Ha ha ha ... Uh, never mind! Whenever you choose to work with PyCharm, make sure you install packages via its integrated terminal."

Alt Text

"Ah, cool! You have made my day! But why is that?"

PyCharm by default will create us a virtual environment every time we create a new project. A virtual environment usually belongs to only one project. Anytime we see a prompt like:

(venv) D:\Testing\test8>
Enter fullscreen mode Exit fullscreen mode

rather than:

D:\Testing\test8>
Enter fullscreen mode Exit fullscreen mode

we are obviously in a virtual environment named "venv". It will isolate the current one's environment from other projects, so there will be no possibility of having dependency issues of installing two or more packages. Most notably, installing packages that are no longer maintained after years. Therefore, the package installation had better to be done in its specific environment.

Of course, we always have the goo-ey (GUI—Graphical User Interface) for it. We can find more interesting finding by reading the PyCharm's official help.

Alt Text

Please do read, read again and read more. It is never too late! A final note, in almost all cases, the best practice is to have a virtual environment for every single project.


Do you have other problems finding packages? Please let me know.

Top comments (0)