Automation Scripts

Reply
Highlighted

NetMRI Python support - quick script to explore the environment

[ Edited ]
Authority
Posts: 51
6242     0

Hello all,

 

I just wanted to share this short script. It gives you some insight into what is all available in the "environment" your python scripts are running in. Like what variables en modules are available, the python version and it's package search path.

Nothing special, but usefull for figuring our how to get going with Python.

Just run it against some random device and have a look at the status log tab.

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
#     true
#
# Script-Variables:
#     $some_script_variable word
#
# END-SCRIPT-BLOCK

from pprint import pprint
import sys
import pkg_resources
import os


print("="*80)
print("available variables")
print("="*80)
pprint(vars())

print("="*80)
print("sys.path (module search paths)")
print("="*80)
pprint(sys.path)

print("="*80)
print("Current working directory")
print("="*80)
pprint(os.getcwd())

print("="*80)
print("Python version")
print("="*80)
pprint(sys.version)

print("="*80)
print("sys.modules (imported and built-in modules)")
print("="*80)
pprint(sys.modules)

print("="*80)
print("Available modules")
print("="*80)
pprint(list(pkg_resources.working_set))

 

Re: NetMRI Python support - quick script to explore the environment

Expert
Posts: 138
6243     0

Very Nice!

 

Does anyone know why the GUI supports 3.4 whilst the Sandbox is 2.7?  I opened a case asking the same question as well.

 

-Lon.

Re: NetMRI Python support - quick script to explore the environment

Expert
Posts: 138
6243     0

Got an answer from support.  (Thanks goes to Prince J.)

 

We have both python 2.7 and python 3.4 installed in the sandbox. This can also be changed as follows:

 

[root@721-Sandbox ~]# python -V
Python 2.7.2

[root@721-Sandbox ~]# alias python=python3
[root@721-Sandbox ~]# python -V
Python 3.4.3
[root@721-Sandbox ~]#

 

-Lon.

Re: NetMRI Python support - quick script to explore the environment

Authority
Posts: 51
6243     0

Indeed, Python versions can be installed side by side if you're little careful. (atleast the minor releases like 2.7, 3.4, 3.5).

 

But, I would advice against permanently aliasing python3 with python. It'll also change the "system python" and might break system tools like Yum.

 

It might not have impact immediately, but might cause issue when the sandbox needs to be reset or upgraded.

Re: NetMRI Python support - quick script to explore the environment

Adviser
Posts: 371
6243     0

For future use "python3 -V" instead of changing the alias Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh
Showing results for 
Search instead for 
Do you mean 

Recommended for You