I'm trying to programatically monitor the Python libraries that are installed on a cluster in Databricks notebooks. For this I have been relying on the following code snippet:
import requests
def get_context():
return dbutils.notebook.entry_point.getDbutils().notebook().getContext()
def get_host_name():
host_name = get_context().tags().get("browserHostName").get()
return host_name
def get_host_token():
return get_context().apiToken().get()
def get_cluster_id():
cluster_id = get_context().tags().get("clusterId").get()
return cluster_id
def get_installed_libraries():
response = requests.get(
f'https://{get_host_name()}/api/2.0/libraries/cluster-status?cluster_id={get_cluster_id()}',
headers={'Authorization': f'Bearer {get_host_token()}'}
).json()
return [x['library']['pypi']['package'] for x in response['library_statuses']]
get_installed_libraries()
This should print all libraries that are installed on the cluster. However, I notice there is no difference whatsoever in the output of the get_installed_libraries() method before and after running a pip-install command such as pip install spacy, the spacy library, even though successfully installed via pip, doesn't show up in the output of the above method.
Edit: The reason I mentioned installation via the pip command is because the focus is to install libraries to the cluster programatically, e.g. using contents of a requirements.txt file. I have the method to perform the installation, but I'm unable to view where the installed libraries end up, since they are clearly not on the cluster, as the above method doesn't print any of these to console.
What am I missing?



