cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
696
Views
2
Helpful
2
Replies

Download Install & import a librairy within the same python script

Jerems
Spotlight
Spotlight

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

2 Replies 2

davidn#
Cisco Employee
Cisco Employee

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.

Jerems
Spotlight
Spotlight

Thanks a lot for your reply. It looks like at the beginning, we are also missing pip:

 

Jerems_0-1683617478139.png

Thanks and Regards,

Jerems