Understanding Why I Couldn't Find Installed Packages
Tags: python, beginners
Published: 2020-12-29 04:15:59

> For you guys, as always, I would recommend to completing [the official tutorial](https://docs.python.org/3/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;

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

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

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

```powershell
> 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
```

_"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](https://www.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:

```powershell
> 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
```

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;

```powershell
> 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
```

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](https://dev-to-uploads.s3.amazonaws.com/i/ecsik14y8r97qp90yiph.png)

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;
4. Scripts from Python 3.7;
2. Python 3.9;
3. 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;

```bash
$ 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)
```

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.

```python
> 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'
```

# Virtual Environments

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o1bhqqyo510n0h96cweb.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/qiaypvudafsdgsg8f29j.png)

_"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](https://dev-to-uploads.s3.amazonaws.com/i/jsui0h3q5lfhf73cqwnr.png)

_"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:

```powershell
(venv) D:\Testing\test8>
```

rather than:

```powershell
D:\Testing\test8>
```

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](https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ld1dwx569giq0llodcu3.png)

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.