Python Forum

Full Version: Using import in python script offline - HELP!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am completely new to the python world so please bear with me, I am hoping someone can help me understand something.

I am currently trying to interact with an API using a python script. The API is for the Zabbix 5.0 application on RHEL 7.9. The goal is to use the python script to add / remove / modify hosts and items to Zabbix. There are a number of different scripts that can achieve this, both on Git and via Zabbix sources.

The issue is the RHEL server is on a closed network, which means no internet connection.

My question is, on a closed off network, how does the 'import' function of the script work ?
so for example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from pyzabbix import ZabbixAPI
import csv
from progressbar import ProgressBar, Percentage, ETA, ReverseBar, RotatingMarker, Timer

zapi = ZabbixAPI("http://localhost/zabbix")
zapi.login(user="Admin", password="zabbix")

arq = csv.reader(open('/tmp/hosts.csv'))

linhas = sum(1 for linha in arq)

f = csv.reader(open('/tmp/hosts.csv'), delimiter=';')
bar = ProgressBar(maxval=linhas,widgets=[Percentage(), ReverseBar(), ETA(), RotatingMarker(), Timer()]).start()
i = 0

for [hostname,ip] in f:
    hostcriado = zapi.host.create(
        host= hostname,
        status= 1,
        interfaces=[{
            "type": 1,
            "main": "1",
            "useip": 1,
            "ip": ip,
            "dns": "",
            "port": 10050
        }],
        groups=[{
            "groupid": 2
        }],
        templates=[{
            "templateid": 10001
        }]
    )


    i += 1
    bar.update(i)

bar.finish
print " "
In the above script, how does the following work offline:

Quote:from pyzabbix import ZabbixAPI
from progressbar import ProgressBar, Percentage, ETA, ReverseBar, RotatingMarker, Timer

Would i need to download all the modules and place them in a specific folder ?

As advised, i am new to this world so please don't hesitate to give me the idiot's guide version!
Any help will be much appreciated :)

Thanks in advance Smile
Importing doesn't go across the network to look up modules (how could it? What if there were network problems? Your programs wouldn't be able to run), so you need to use pip to install them locally. There are "site-packages" directories in which libraries are installed (e.g. my user one is ~/.local/lib/python3.6/site-packages) and importing looks for modules in those. You may be able to copy the contents of that to the machines on which you're running the programs. Another option, if possible, is to build a Docker image containing the program and all its dependencies and then run a container on the target machine (of course this requires Docker to be installed on the target).