Python Setup

Python Notes

13 Mar 2026

globals()
locals()
dir()

#example
global g
g=45
repr(g)
'45'




A Python virtual environment is a folder structure that gives you everything you need to run a lightweight yet isolated Python environment. https://realpython.com/python-virtual-environments-a-primer/

List packages in a virtual environment

python -m pip list

Using Python virtual Environment from command line (Windows Powershell)

  1. Loosen Your Execution Policy

The execution policy sets how strict your system is about running scripts from other sources. For this tutorial, you’ll want to set it to RemoteSigned:

PS> Set-ExecutionPolicy RemoteSigned
  1. Create Environment named venv

    PS C:\source_py\E1> py -m venv venv\
    
  2. Activate it

    PS C:\source_py\E1> venv\Scripts\activate
    (venv) PS C:\source_py\E1>
    
  3. Install Packages

(venv) PS> python -m pip install <package-name>
or
PS C:\source_py\E1> pip install <package-name>
#example
PS C:\source_py\E1> pip install paramiko
PS C:\source_py\E1> pip install netmiko
PS C:\source_py\E1> pip install wxPython
  1. Deactivate it (at some point)

    (venv) PS> deactivate
    PS>
    
  2. Backup your env (PIN it) including all loaded modules via:

(venv) PS> python -m pip freeze > requirements.txt
  1. Use your pinned environment to re-create it elsewhere (venv) PS> deactivate PS> py -m venv new-venv\ PS> new-venv\Scripts\activate (new-venv) PS> python -m pip install -r requirements.txt

SSH libs - paramiko and netmiko (which depends on paramiko)

From within the venv

(venv) PS C:\source_py\E1> pip install paramiko

Then you can use it as follows (contents of sshTest.py)

import paramiko

# Connection details
hostname = '192.168.1.141'
username = 'tech'
password = 'Password123'  # Use SSH keys for better security
port = 22



# Create an SSH client
ssh_client = paramiko.SSHClient()

# Automatically add the server's host key (use with caution in production)
# For better security, manage known_hosts file manually or use WarningPolicy
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    # Connect to the remote server
    ssh_client.connect(hostname, port, username, password)

    # Command to execute on the remote machine (e.g., listing directory contents)
    command = 'ls -l'  # Or 'dir' if connecting to a Windows machine running an SSH server

    # Execute the command
    stdin, stdout, stderr = ssh_client.exec_command(command)

    # Read and print the output
    print(f"Executing command: {command}")
    print("--- STDOUT ---")
    for line in stdout:
        print(line.strip())

    print("--- STDERR ---")
    for line in stderr:
        print(line.strip())

    # Get the exit status
    exit_status = stdout.channel.recv_exit_status()
    print(f"Command finished with exit status: {exit_status}")

except paramiko.AuthenticationException:
    print("Authentication failed, please check your credentials")
except paramiko.SSHException as e:
    print(f"SSH connection failed: {e}")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Close the connection
    if ssh_client:
        ssh_client.close()
        print("SSH connection closed.")


Install netmiko

(venv) PS C:\source_py\E1> pip install netmiko

Then you can use it as follows (contents of netMikotest.py)

from netmiko import ConnectHandler

SW_01 = {
    "device_type": "linux",
    "host": "192.168.1.22",
    "username": "tadmin",
    "password": "peanuts"
}


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

    connection = ConnectHandler(**SW_01)
    #connection = ConnectHandler(host="192.168.1.22", username="tadmin", password="peanuts", device_type="linux")
    output = connection.send_command("pwd")
    print(output)

    output = connection.send_command("ls -l")
    print(output)

    output = connection.send_command("uname")
    print(output)

    connection.disconnect()

Create Single EXE

pip install pyinstaller
pyinstaller --onefile main.py

of

pyinstaller --onefile --windowed --icon "CartoonLegs.ico" main.py

Use of requirements.txt for pyuibuilder

To get started install the requirements.txt if any, using the command

pip install -r requirements.txt

Map

https://github.com/TomSchimansky/TkinterMapView/tree/main?tab=readme-ov-file


from pprint import pprint
pprint(locals())

vars()

dir()




Links in work

https://www.google.com/search?q=python+embed+png+as+code&sca_esv=dba3b81a402183e1&biw=2327&bih=1405&sxsrf=ANbL-n6URcFpgoC35giA4LH6FBPoF8OnYw%3A1775166558857&ei=XuTOafz0M6u5p84Piv-ZkQk&ved=0ahUKEwj8m6HhktCTAxWr3MkDHYp_JpIQ4dUDCBE&uact=5&oq=python+embed+png+as+code&gs_lp=Egxnd3Mtd2l6LXNlcnAiGHB5dGhvbiBlbWJlZCBwbmcgYXMgY29kZTIFECEYoAEyBRAhGJ8FMgUQIRifBUjtGlCXBFibGXABeACQAQCYAWWgAbQKqgEEMTYuMbgBA8gBAPgBAZgCEqAC5ArCAg4QABiABBiwAxiGAxiKBcICCBAAGLADGO8FwgILEAAYgAQYsAMYogTCAgsQABiwAxiiBBiJBcICBBAjGCfCAgsQABiABBiRAhiKBcICChAAGIAEGEMYigXCAggQABiABBixA8ICBRAAGIAEwgIKEAAYgAQYFBiHAsICCRAAGIAEGAoYC8ICCBAAGBYYChgewgIGEAAYFhgewgILEAAYgAQYhgMYigXCAgUQABjvBcICCBAAGIAEGKIEwgIEECEYFZgDAIgGAZAGCJIHBDE2LjKgB5hbsgcEMTUuMrgH4QrCBwYxLjE2LjHIBx6ACAA&sclient=gws-wiz-serp

https://www.google.com/search?q=netmiko+is+connection+open&sca_esv=b78cf8500232fcdc&sxsrf=ANbL-n4GclQNVe1cU8F98sdsE8JjTBum3g%3A1775085674626&ei=aqjNae3xJY3l5NoP4ejTmQc&biw=1493&bih=1005&ved=0ahUKEwjtztO45c2TAxWNMlkFHWH0NHMQ4dUDCBE&uact=5&oq=netmiko+is+connection+open&gs_lp=Egxnd3Mtd2l6LXNlcnAiGm5ldG1pa28gaXMgY29ubmVjdGlvbiBvcGVuMggQIRigARjDBEjiG1DAB1jcGHACeAGQAQCYAWygAY0FqgEDNy4xuAEDyAEA-AEBmAIKoAKnBcICChAAGLADGNYEGEfCAggQABgHGAgYHsICCxAAGIAEGIYDGIoFwgIFEAAY7wXCAgoQIRigARjDBBgKmAMA4gMFEgExIECIBgGQBgiSBwM5LjGgB68psgcDNy4xuAehBcIHBTEuOC4xyAcQgAgA&sclient=gws-wiz-serp

https://www.google.com/search?q=python+show+vars&sca_esv=b78cf8500232fcdc&sxsrf=ANbL-n7kwngAVVjYImNumgkJwASLtXqaxQ%3A1775090877284&ei=vbzNaYj9EM2o5NoPyceBwAQ&biw=1493&bih=1005&ved=0ahUKEwjIz7zp-M2TAxVNFFkFHcljAEgQ4dUDCBE&uact=5&oq=python+show+vars&gs_lp=Egxnd3Mtd2l6LXNlcnAiEHB5dGhvbiBzaG93IHZhcnMyBhAAGAgYHjIGEAAYCBgeMggQABiABBiiBDIFEAAY7wUyCBAAGKIEGIkFMgUQABjvBUjyKVDJB1i-KHACeAGQAQCYAVygAaMFqgEBObgBA8gBAPgBAZgCC6ACygXCAgoQABiwAxjWBBhHwgIKECMYgAQYJxiKBcICBBAjGCfCAg0QABiABBixAxgUGIcCwgIFEAAYgATCAgsQABiABBiRAhiKBcICChAAGIAEGBQYhwLCAgsQLhiABBjRAxjHAcICDBAAGIAEGLEDGAoYC8ICCRAAGIAEGAoYC8ICBRAhGKABwgIHEAAYgAQYDcICBhAAGBYYHsICBRAhGJ8FmAMAiAYBkAYIkgcCMTGgB6k9sgcBObgHxQXCBwUwLjguM8gHGoAIAA&sclient=gws-wiz-serp

https://docs.python.org/3/using/cmdline.html

https://github.com/rdbende/Azure-ttk-theme

https://github.com/rdbende/tkinter-toolkit

https://github.com/TomSchimansky/CustomTkinter


You'll only receive email when they publish something new.

More from Brad G
All posts