05-08-2023 04:54 AM
Hi dear community,
I wondered if it was possible within the same python script to download, install and import a python librairy that is not yet part of the librairies available within the guestshell environment on a Cisco router.
I aim to trigger a python script (downloaded thanks to dhcp options) which requires some additionnal librairies so it can be executed properly. This script is executed by a cisco router at bootup (ZTP operation). The "cli" helps by allowing the execution of some shell command line like :
"guestshell run sudo python3 -m pip install requests"
Within the same script, i would need to import the newly downloaded requests librairy.
Can you please advise ?
Thanks and Regards,
Jerems
05-08-2023 09:21 AM
Not sure about using guestshell to install the required libraries but in the Python script you could use the following to detect and install the missing library:
import subprocess
# Check if requests library is installed
try:
import requests
print("Requests library is already installed.")
except ImportError:
# Install requests library using pip
print("Requests library is not installed. Installing now...")
subprocess.check_call(["pip", "install", "requests"])
print("Requests library has been installed.")
Hope that help.
05-09-2023 12:31 AM
Thanks a lot for your reply. It looks like at the beginning, we are also missing pip:
Thanks and Regards,
Jerems
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide