Python Setup
April 29, 2026•605 words
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)
- 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
Create Environment named venv
PS C:\source_py\E1> py -m venv venv\Activate it
PS C:\source_py\E1> venv\Scripts\activate (venv) PS C:\source_py\E1>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
Deactivate it (at some point)
(venv) PS> deactivate PS>Backup your env (PIN it) including all loaded modules via:
(venv) PS> python -m pip freeze > requirements.txt
- 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://docs.python.org/3/using/cmdline.html
https://github.com/rdbende/Azure-ttk-theme