Bootstrap FreeKB - Python (Scripting) - Resolve "ImportError: No module named"
Python (Scripting) - Resolve "ImportError: No module named"

Updated:   |  Python (Scripting) articles

Let's say you are getting something like this.

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    import boto3
ImportError: No module named boto3

 

In this example, "import boto3" on line 2 of example.py is returning the import error.

#!/usr/bin/python
import boto3

 

Use the pip list command to determine if the package is installed.

~]$ pip list
Package             Version
------------------- ---------
boto3               1.28.33

 

If the package is not listed, use the pip install command to install the package.

pip install boto3

 

Or, better yet, use a requirements.txt file.

boto3==1.28.33

 

And then install the packages using the requirements.txt file.

pip install --requirement requirements.txt

 

If the issue persists, use the pip --version command to determine the version of Python associated with pip.

~]$ pip --version
pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

 

And compare this to the version of Python returned by shebang line 1 in your Python program. You may just need to update your shebang line 1 from python to python3 (e.g. #!/usr/bin/python3).

~]$ /usr/bin/python --version
Python 2.7.5

 

Likewise, this may occur if you use python version 2 to invoke the Python program instead of python3.

~]$ python example.py 
Traceback (most recent call last):
  File "example.py", line 2, in <module>
    import boto3
ImportError: No module named boto3

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 502a95 in the box below so that we can be sure you are a human.