A popular problem for new Ubuntu users is installation of Python 3 packages like Pandas.

If you try to install Pandas for Python 3.6, 3.7 or 3.8 by:

pip3 install pandas

and then if you try to use pandas as:

import pandas as pd

in a script like(or called in cron job):

python3 /home/scripts/update_party.py

and get Python error like:

ModuleNotFoundError: No module named 'pandas'

then this article is for you.

This article is applicable for Ubuntu, Linux Mint and Debian.

Solution 1: Install Pandas by PIP and setting Home

If you like to install Pandas on the system Python 3 by PIP and run scripts with it then you can install Python packages by PIP with setting home option:

sudo apt-get install python3-pip
sudo -H pip3 install pandas

Note: It might be a bad idea to use sudo and pip. It's a good idea to use virtual environments for custom Python scripts.

Pro tip: If you like to check what option -H is doing you can do:
man sudo

-H, --set-home
Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database
entry. Depending on the policy, this may be the default behavior.

So flag -H install the package in you proper home folder

Solution 2: Install Python package from Ubuntu repository

The second option is to check if the package is available in the official Ubuntu repository. If this is the case than you can install Python package from Ubuntu repository by (you can install Python packages for other Linux distributions too):

sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
sudo apt-get install python3-pandas

Note: You can check what is the version of the module. In some cases the repository will be several version behind latest version available from pip install. You can check the package version by:

pip3 freeze | grep pandas

result:

pandas==1.1.3

Step 3: How to investigate the problem

Sometimes the error might be slightly different. In this case investigation will help to resolve the problem.

In case of a Cron job error you can check this article: [Linux Mint: How to Check Cron Logs (crontab -e)]/linux-mint-how-to-check-cron-logs-crontab-e/). In this article you can find how to find the logs and read errors produced by Cron jobs.

Next you can check the python versions by:

$ python -V
$ python3 -V

result

Python 2.7.17
Python 3.6.9

Next steps is to check what packages are installed for the give version: Python 3 for example:

$pip3 freeze

output:

acme==0.31.0
asn1crypto==0.24.0
attrs==17.4.0
Automat==0.6.0
blinker==1.4

And finally you can check what Python installation is used in your Ubuntu or Linux Mint by:

which python3pip3 freeze | grep pandas
pandas==1.1.3

Result

/usr/bin/python3