Bootstrap FreeKB - Python (Scripting) - List Python system paths using sys.path
Python (Scripting) - List Python system paths using sys.path

Updated:   |  Python (Scripting) articles

Python modules are typically located somewhere below sys.path.

Here is how you would determine the system paths.

#!/usr/bin/python3
import sys

print("System Paths:")

for directory in sys.path:
  print(f"  {directory}")

 

Which should return something like this.

System Paths:
  /home/john.doe
  /usr/lib64/python27.zip
  /usr/lib64/python2.7
  /usr/lib64/python2.7/plat-linux2
  /usr/lib64/python2.7/lib-tk
  /usr/lib64/python2.7/lib-old
  /usr/lib64/python2.7/lib-dynload
  /usr/lib64/python2.7/site-packages
  /usr/lib/python2.7/site-packages

 

However, let's say you have created your own module that contains functions and you want other Python scripts to be able to use the functions. If the Python module you created does not reside in one of the system paths, the following can be included in the other Python scripts to append the directory that contains the "master" Python script to the system paths. However, this is more of a temporary solution rather than a permanent solution.

sys.path.append("/tmp")

 

One permanent solution would be to move the Python module you created into one of the sys.path directories, such as /usr/lib64/python2.7/site-packages.

 




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