- Find the Location of Python site-packages Directory
- 1. site Module to Find Directory of Python Site-Packages
- 2. sys Module – Python Site-Packages Directory
- 3. Using os Module
- 4. distutils – Find Directory of “site-packages”
- 5. Use Editor – The Optimal and Easy Way
- 6. Summary and Conclusion
- You may also like reading:
- AlixaProDev
- Where does Python look for modules?¶
- Python looks for modules in “sys.path”¶
Find the Location of Python site-packages Directory
How do I find the location of my Python site-packages directory? The “site-packages” directory is where third-party packages are installed for Python. In this article, we will explain different methods to find the location of the “site-packages” directory.
A quick way to find the “site-packages” directory is by using the site module. You can simply import the module and use the getsitepackages() function to get the “site-packages” directory. However, we will discuss more methods like, the sys module, the os module, and the distutils package with examples.
1. site Module to Find Directory of Python Site-Packages
The site module allows you to access the python interpreter and the installed packages, one of the functions provided by the site module is getsitepackages() , which returns a list of directories that contain the installed packages, including the “site-packages” directory.
See the following code example:
# Import site module import site # Get the list of directories site_packages_dirs = site.getsitepackages() # Find the "site-packages" directory in the list for dir in site_packages_dirs: if dir.endswith("site-packages"): target_dir = dir break else: target_dir=None print(target_dir)
# Output: c:\Users\Ali\Desktop\SparkByExamples\env\Lib\site-packages
There are two types of python “site-packages” directories. One is called the global site-packages directory and the virtual environment “site-packages”.
You can run the following command on your terminal to find the directory of python global site-packages.
# Find Location of Global "site-packages" python -m site
# Output: c:\Users\Ali\AppData\Local\Programs\Python\Python311\Lib\site-packages
This will return the location of the global directory of python “site-packages”. You can also add another parameter to get the location per user.
#Find Location of Per-user "site-packages" python -m site --user-site
2. sys Module – Python Site-Packages Directory
The sys module is another way to access various system-specific parameters and functions. One of the attributes provided by the sys module is path , which is a list of directories that Python searches for modules.
# Find the "site-packages" directory in the "sys.path" list import sys # Iterate over the Directories for dir in sys.path: if dir.endswith("site-packages"): target_dir = dir else: target_dir = None print(target_dir)
# Output: c:\Users\Ali\Desktop\SparkByExamples\env\Lib\site-packages
3. Using os Module
The os module is a built-in module in Python that provides a way to interact with the operating system. The os module provides several functions for navigating the file system and getting information about files and directories. One of these functions is listdir() which returns a list of the files and directories in a specified directory.
# Using os to get the Location import os dir_path = os.path.dirname(os.__file__) + '/site-packages' print(dir_path)
# Output: C:\Users\Ali\AppData\Local\Programs\Python\Python311\Lib\site-packages
4. distutils – Find Directory of “site-packages”
The distutils module in Python provides support for building and distributing Python modules. It includes a sysconfig module, which provides functions for retrieving configuration information about the Python installation.
# distutils library to get the location of Python "site-packages" from distutils.sysconfig import get_python_lib site_packages_dir = get_python_lib() print(site_packages_dir)
# Output: c:\Users\Ali\Desktop\SparkByExamples\env\Lib\site-packages
The sysconfig is also available is a standalone module, which you can use directly instead of importing it from the distutil .
# Using the sysconfig module import sysconfig print(sysconfig.get_paths())
The location return by the sysconfig on Debian seems like to be not available, this is because when you install packages using pip, it will go into ‘dist-packages’ and not to ‘site-packages’.
5. Use Editor – The Optimal and Easy Way
If you are using a virtual environment for your project, you can find the python site packages in the editor. The editor maybe is of your choice. In my case, I am using VS code and my virtual environment is “env”. You can see the complete path to the “site-packages” directory.
6. Summary and Conclusion
In this article, we have learned how to find the location of the python “site-packages” directory. You have learned multiple ways to do this. If you are working in VS Code or any other editor along with a virtual environment, this directory is already available to you. Hope this article was helpful. Leave your questions in the comment section.
You may also like reading:
AlixaProDev
I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.
Where does Python look for modules?¶
Let’s say we have written a Python module and saved it as a_module.py , in a directory called code .
We also have a script called a_script.py in a directory called scripts .
We want to be able to import the code in a_module.py to use in a_script.py . So, we want to be able to put his line in a_script.py :
The module and script might look like this:
def func(): print("Running useful function")
import a_module a_module.func()
At the moment, a_script.py will fail with:
$ python3 scripts/a_script.py Traceback (most recent call last): File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'
When Python hits the line import a_module , it tries to find a package or a module called a_module . A package is a directory containing modules, but we will only consider modules for now. A module is a file with a matching extension, such as .py . So, Python is looking for a file a_module.py , and not finding it.
You will see the same effect at the interactive Python console, or in IPython:
>>> import a_module Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'a_module'
Python looks for modules in “sys.path”¶
Python has a simple algorithm for finding a module with a given name, such as a_module . It looks for a file called a_module.py in the directories listed in the variable sys.path .
>>> import sys >>> type(sys.path) >>> for path in sys.path: . print(path) . /Users/brettmz-admin/dev_trees/psych-214-fall-2016/sphinxext /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Users/brettmz-admin/Library/Python/3.7/lib/python/site-packages /Users/brettmz-admin/dev_trees/grin /Users/brettmz-admin/dev_trees/rmdex /usr/local/lib/python3.7/site-packages
The a_module.py file is in the code directory, and this directory is not in the sys.path list.
Because sys.path is just a Python list, like any other, we can make the import work by appending the code directory to the list.
>>> import sys >>> sys.path.append('code') >>> # Now the import will work >>> import a_module
There are various ways of making sure a directory is always on the Python sys.path list when you run Python, including:
- put the directory into the contents of the PYTHONPATH environment variable – Using PYTHONPATH
- make the module part of an installable package, and install it – see: making a Python package.
As a crude hack, you can also put your code directory on the Python sys.path at the top of the files that need it:
import sys sys.path.append('code') import a_module a_module.func()
$ python3 scripts/a_script_with_hack.py Running useful function
The simple append above will only work when running the script from a directory containing the code subdirectory. For example:
$ mkdir another_dir $ cd another_dir $ python3 ../scripts/a_script_with_hack.py Traceback (most recent call last): File "../scripts/a_script_with_hack.py", line 4, in import a_module ModuleNotFoundError: No module named 'a_module'
This is because the directory code that we specified is a relative path, and therefore Python looks for the code directory in the current working directory.
To make the hack work when running the code from any directory, you could use some path manipulation on the The “__file__” variable :
from os.path import dirname, abspath, join import sys # Find code directory relative to our directory THIS_DIR = dirname(__file__) CODE_DIR = abspath(join(THIS_DIR, '..', 'code')) sys.path.append(CODE_DIR) import a_module a_module.func()
Now the module import does work from another_dir :
$ python3 ../scripts/a_script_with_better_hack.py Running useful function