Utils
Pyloid provides utility functions to help with resource path resolution, environment detection, platform identification, and networking.
Production & Development
get_production_path
Constructs the absolute path to a resource file, adjusting based on the execution environment.
- In a production environment (e.g., when packaged with PyInstaller), it prepends the application's base directory to the provided relative path.
- If no path is provided, it returns the base path itself.
- In a development environment (regular Python script), it simply returns the provided path as is. If no path is provided, it returns
None.
from pyloid.utils import get_production_path
########## Usage ##########
# In production (e.g., PyInstaller bundle, sys._MEIPASS = '/tmp/_MEIabcde')
prod_path = get_production_path("assets/icon.ico")
print(prod_path) # '/tmp/_MEIabcde/assets/icon.ico'
base_prod_path = get_production_path()
print(base_prod_path) # '/tmp/_MEIabcde'Production Example
The example shows how get_production_path() automatically returns the correct path in both development (dev) and bundled (prod) environments, allowing you to use the same code without additional branching.
from pyloid import Pyloid
from pyloid.utils import get_production_path
app = Pyloid(app_name="MyApp", single_instance=True)
app.set_icon(get_production_path("assets/icon.ico")) # set the icon
app.set_tray_icon(get_production_path("assets/icon.ico")) # set the tray iconParameters
Prop
Type
Returns
Optional[str]If in production: The absolute path to the resource file (or the base path if path is None). If not in production: The original path value passed to the function.is_production
Checks if the current environment is a production environment (e.g., PyInstaller or Nuitka bundle).
- Returns
Trueif running in a production environment, otherwiseFalse.
from pyloid.utils import is_production
########## Usage ##########
if is_production():
print("Running in production environment.")
else:
print("Not in production environment.")Returns
boolTrue if in a production environment, False otherwise.Platform
get_platform
Returns the name of the current system's platform.
- Uses
platform.system()to determine the OS. - Returns one of:
"windows","macos","linux".
from pyloid.utils import get_platform
########## Usage ##########
platform_name = get_platform()
print(platform_name) # e.g., "windows"Returns
strPlatform name: "windows", "macos", or "linux".Path Operations
get_absolute_path
Returns the absolute path of the given relative path.
from pyloid.utils import get_absolute_path
########## Usage ##########
absolute_path = get_absolute_path("assets/icon.ico")
print(absolute_path)
# e.g., 'C:/Users/aaaap/Documents/pyloid/pyloid/assets/icon.ico'Parameters
Prop
Type
Returns
strThe absolute path.Networking
get_free_port
Finds and returns an available random network port number from the operating system.
- Creates a socket, binds to port
0(let OS choose), retrieves the port, and closes the socket. - Note: The port may be reassigned to another process after this function returns, so use it quickly.
from pyloid.utils import get_free_port
########## Usage ##########
port = get_free_port()
print(f"Found available port: {port}")
# Example: Start a web server on the found port
import http.server
server = http.server.HTTPServer(('localhost', port), http.server.SimpleHTTPRequestHandler)Returns
intAn available network port number (typically in the range 1024-65535).Qt Backend
set_qt_backend
Sets the Qt Quick backend to force a specific rendering mode.
- Allows changing the Qt Quick rendering backend by setting the
QT_QUICK_BACKENDenvironment variable. - Setting it to
'software'forces software rendering, which can be useful in environments with graphics driver issues or where hardware acceleration is not available. - Important: This setting must be applied before the Qt application is initialized.
from pyloid.utils import set_qt_backend
########## Usage ##########
# Force software rendering (useful for compatibility)
set_qt_backend('software')
# You can also use other backends like 'opengl' or 'd3d12' if needed
# set_qt_backend('opengl')Production Example
This is typically called at the very beginning of your application, before creating any Pyloid instances:
from pyloid.utils import set_qt_backend
from pyloid import Pyloid
# Set software rendering for better compatibility
set_qt_backend('software')
app = Pyloid(app_name="MyApp", single_instance=True)
# Rest of your application code...Parameters
Prop
Type
Returns
NoneThis function does not return a value.