Python Forum
Using import in python script offline - HELP!!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using import in python script offline - HELP!!
#1
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
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,181 Jun-29-2023, 11:57 AM
Last Post: gologica
Question How can I import a variable from another script without executing it ThomasFab 12 7,745 May-06-2022, 03:21 PM
Last Post: bowlofred
  Import output from python script to another varietyjones 1 1,896 Oct-12-2020, 09:07 PM
Last Post: bowlofred
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,875 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Generate RPM package from a Python + Kivy application for an "offline" installation pruvosim 2 2,199 Jun-04-2020, 12:16 PM
Last Post: pruvosim
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,288 May-28-2020, 05:27 PM
Last Post: micseydel
  Python zeep offline installation on RHEL akinmhmt 0 1,994 Mar-26-2020, 09:04 AM
Last Post: akinmhmt
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,617 May-11-2019, 08:12 PM
Last Post: keames
  Install Python Offline DTMT 2 2,276 Mar-20-2019, 04:03 PM
Last Post: snippsat
  [split] Offline audio to text (Speech Recognition) Nishant260190 0 3,922 Sep-02-2018, 12:33 PM
Last Post: Nishant260190

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020