cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2665
Views
0
Helpful
3
Replies

Importing external Python libraries

martin.gysi
Level 1
Level 1

I'd like to use the 'ipaddress' python library in an NSO package. Normally, I'd do 'pip2 install ipaddress' in a virtualenv to install the library and then import it using 'import ipaddress' in my python code.

 

How can external Python libraries be added and used with the Python-VM of NSO?

1 Accepted Solution

Accepted Solutions

vleijon
Cisco Employee
Cisco Employee

The simplest way is to make sure that PYTHONPATH is set correctly when starting NSO.

 

More advanced, there is a setting set python-vm start-command, by default it runs 

$NCS_DIR/bin/ncs-start-python-vm.

 

There is some additional documentation in the developer's guide on how to go about changing which python is started.

View solution in original post

3 Replies 3

vleijon
Cisco Employee
Cisco Employee

The simplest way is to make sure that PYTHONPATH is set correctly when starting NSO.

 

More advanced, there is a setting set python-vm start-command, by default it runs 

$NCS_DIR/bin/ncs-start-python-vm.

 

There is some additional documentation in the developer's guide on how to go about changing which python is started.

Thanks. I added the path to my library to PYTHONPATH in $NCS_DIR/bin/ncs-start-python-vm:

 

#!/bin/sh

pypath="${NCS_DIR}/src/ncs/pyapi"
ipaddrpath="/Users/tgdgyma/ipaddr/lib/python2.7/site-packages"

 

# Make sure everyone finds the NCS Python libraries at startup
if [ "x$PYTHONPATH" != "x" ]; then
PYTHONPATH=${ipaddrpath}:${pypath}:$PYTHONPATH
else
PYTHONPATH=${ipaddrpath}:${pypath}
fi
export PYTHONPATH

main="${pypath}/ncs_pyvm/startup.py"

echo "Starting ${main} $*"
exec python -u ${main} $*

That will work well for development, but it isn’t recommended for system installation because it is easy to lose the script during upgrades and such, then it is better to make a copy in a different location and set the start command to point to it.