w3resource

Python: Locate Python site-packages

Python Basic: Exercise-44 with Solution

Write a Python program to locate Python site packages.

site.getsitepackages(): Return a list containing all global site-packages directories.

Sample Solution:

Python Code:

# Import the 'site' module.
import site

# Use the 'site.getsitepackages()' function to retrieve site packages' paths.
# 'site.getsitepackages()' returns a list of paths where site packages are installed.
print(site.getsitepackages())

Sample Output:

['/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.5/dist-packages
']

Explanation:

The above Python code imports the ‘site’ module and then calls the getsitepackages() function.

By calling print(site.getsitepackages()), the code will print a list of these paths to the console output. This can be useful for understanding where Python packages are installed on a particular system, which can be important for managing dependencies in Python projects.

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get OS name, platform and release information.
Next: Write a python program to call an external command in Python.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.