w3resource

Python: Locate Python site-packages


Python Site Packages Locator

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.

For more Practice: Solve these Related Problems:

  • Write a Python script to list all installed Python packages along with their versions.
  • Write a function that finds the location of a specific installed Python package.
  • Write a script to check if a particular package is installed and return its path.
  • Write a Python program that determines the default Python module search paths.

Go to:


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.

Python Code Editor:

 

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.