Bootstrap FreeKB - Python (Scripting) - import module
Python (Scripting) - import module

Updated:   |  Python (Scripting) articles

The import keyword is used to import a module. There are several built in modules that are part of the Python Standard Library included with your Python installation, such as os (Operating System) and re (Regular Expression) and platform. For example, here is how you would import and then use the platform module.

#!/usr/bin/python3
import platform

print(f"Platform = {platform.system()}")

 

Running this script should return something like this.

Platform = Linux

 

What has happened here is that the platform.py module was used. The platform.py module should be found somewhere in sys.path.

 


ImportError: No module named

Let's say you try to import a bogus platform.

import foo

 

Running this script should return something like this.

Traceback (most recent call last):
  File "testing.py", line 1, in <module>
    import foo
ImportError: No module named foo

 

However, you may also get ImportError: No module named for a valid module that has not been installed.

import jsonpatch

 

For example, this could occur with the jsonpatch module.

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

 

The pip list command will display the packages that have been installed by pip. Here is an example of what could be returned by the pip list command.

AVOID TROUBLE

There are 3 versions of pip

  • pip is used for Python version 2.6 and below
  • pip2 is used for Python version 2.7 and above
  • pip3 is used for Python version 3

For example, the /usr/bin/pip command (or just pip) would be used for Python version 2.6 and below.

~]$ pip list
Package            Version
------------------ ---------
boto3              1.23.10
botocore           1.26.10
certifi            2021.10.8
cffi               1.15.0
charset-normalizer 2.0.12
cryptography       36.0.1
idna               3.3
jmespath           0.10.0
pip                21.3.1
pycparser          2.21
PyJWT              2.3.0
python-dateutil    2.8.2
requests           2.27.1
s3transfer         0.5.2
setuptools         39.2.0
six                1.16.0
urllib3            1.26.8

 

The pip install command can be used to install a package.

]$ pip install jsonpatch
Defaulting to user installation because normal site-packages is not writeable
Collecting jsonpatch
  Downloading jsonpatch-1.32-py2.py3-none-any.whl (12 kB)
Collecting jsonpointer>=1.9
  Downloading jsonpointer-2.3-py2.py3-none-any.whl (7.8 kB)
Installing collected packages: jsonpointer, jsonpatch
Successfully installed jsonpatch-1.32 jsonpointer-2.3

 

Let's say you've created a Python file named my_functions.py that contain functions you want to use with multiple different Python files. Here is how you can import the functions in my_functions.py into your Python file.

import sys
sys.path.append("/use/local/my/modules")
from my_functions import *

 




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 da2044 in the box below so that we can be sure you are a human.