Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Standalone application
#1
Lightbulb 
Hello dear coders, i was searching way to print in console CPU temperature and i found code i put bellow. The problem is, this code works only when i have in my PC OpenHardwareMonitor program.
THIS - https://openhardwaremonitor.org/documentation/
So in my opinion this code just takes information from this program. When i make standalone EXE. with Pyinstaller from this script and run it on second computer without open hardwaremonitor it is not working.

PLEASE GUYS IS THERE ANY SOLUTION TO RUN THIS CODE ON EVERY MACHINE ? HOW TO ADD OpenHardwareMonitor to dependencies or is there any other way to make this working alone ? Thank you.

import wmi
w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()


for sensor in temperature_infos:
    if sensor.Name=='CPU Core':
        print(sensor.Value)
        break
Iam also adding photo of how this program looks like

app
Reply
#2
You could perhaps replace this by a call to psutils.sensors_temperatures()

In linux, it works well.
Reply
#3
On windows not working. Already tried. Thank you anyway.
Reply
#4
(Jul-31-2020, 06:39 PM)samuelbachorik Wrote: PLEASE GUYS IS THERE ANY SOLUTION TO RUN THIS CODE ON EVERY MACHINE ? HOW TO ADD OpenHardwareMonitor to dependencies or is there any other way to make this working alone ? Thank you.
It's possible,but there are step that need to be understand and taken.
First have to check if OpenHardwareMonitor is running,if not start it(minimized) and wait some time for it to open.
Here i check with psutil if it running,if not start it,then wait for it to start then run code for it.
Also close it when leave the code or it gone start a new process(OpenHardwareMonitor) every time run it.
# open_temp.py
import time, os
import psutil
import subprocess
import wmi

def check_process():
    proc_name = "OpenHardwareMonitor.exe"
    for proc in psutil.process_iter():
        if proc.name() == proc_name:
            pass
    else:
        subprocess.Popen(proc_name)

def open_hardware():
    w = wmi.WMI(namespace="root\OpenHardwareMonitor")
    temperature = w.Sensor()
    for sensor in temperature:
        if sensor.SensorType == 'Temperature':
            print(sensor.Name)
            print(sensor.Value)
    input('Press Enter to exit')
    subprocess.run(['taskkill.exe', '/IM', 'OpenHardwareMonitor.exe'])

if __name__ == '__main__':
    check_process()
    time.sleep(5)
    open_hardware()
All files from OpenHardwareMonitor and open_temp.py in same folder.
Build with.
G:\div_code\open_hardware
λ pyinstaller --onedir --add-data "G:\\div_code\\open_hardware\\*;." open_temp.py
131 INFO: PyInstaller: 3.6
132 INFO: Python: 3.8.3
132 INFO: Platform: Windows-10-10.0.18362-SP0
135 INFO: wrote G:\div_code\open_hardware\open_temp.spec
.....
18307 INFO: Building COLLECT COLLECT-00.toc completed successfully.
open_temp.exe need to be run elevated,also as administrator.
Reply
#5
Thank you a lot ! Understand everything exepct pyinstaller code.

pyinstaller --onedir --add-data "G:\\div_code\\open_hardware\\*;." open_temp.py
In this code ""G:\\div_code\\open_hardware\\*;."" what this mean ? It is path to some file ? I dont get it from where you have open_hardware file.
Reply
#6
Quote:In this code ""G:\\div_code\\open_hardware\\*;."" what this mean ? It is path to some file ? I dont get it from where you have open_hardware file.
File structure.
G:\div_code\
  |-- open_hardware\
    |-- open_temp.py
    |-- OpenHardwareMonitor.exe
    |-- OpenHardwareMonitor.config
    |-- rest of files from OpenHardwareMonitor
So all is done from command line,here some navigate i use cmder use dir instead ls in cmd.
G:\div_code
λ cd open_hardware\

# list files in open_harware folder
G:\div_code\open_hardware
λ ls
Aga.Controls.dll*           OpenHardwareMonitor.exe*     OxyPlot.dll*  dist/           temp_show.py
License.html                OpenHardwareMonitorLib.dll*  __pycache__/  open_temp.py
OpenHardwareMonitor.config  OxyPlot.WindowsForms.dll*    build/        open_temp.spec

# Test that pyinstaller is installed
G:\div_code\open_hardware
λ pyinstaller --version
3.6

# Test python
G:\div_code\open_hardware
λ python -V
Python 3.8.3

# Cd to dist
G:\div_code\open_hardware
λ cd dist

G:\div_code\open_hardware\dist
λ ls
open_temp/

G:\div_code\open_hardware\dist
λ cd open_temp\

# Test that open_temp.exe work
G:\div_code\open_hardware\dist\open_temp
λ open_temp.exe
CPU Core #3
39.0
CPU Core #4
37.0
CPU Core #5
43.0
CPU Core #6
43.0
Temperature
29.0
GPU Core
32.0
Temperature
29.0
Auxiliary
37.5
System
34.0
CPU Core #1
49.0
CPU Core #2
40.0
Press Enter to exit
SUCCESS: Sent termination signal to the process "OpenHardwareMonitor.exe" with PID 39700.

G:\div_code\open_hardware\dist\open_temp
λ
So in code over this command under here is already run.
This command means take all files \\* in folder open_hardware folder and place it in root folder . of pyinstaller dist\open_temp folder.
G:\div_code\open_temp folder.
λ pyinstaller --onedir --add-data "G:\\div_code\\open_hardware\\*;." open_temp.py
Reply
#7
If i have hardwaremonitor on my desktop in windows, my code will look like this ?

pyinstaller --onedir --add-data "C:\Users\Samuel\Desktop\OpenHardwareMonitor\\*;." open_temp.py

I think i still dont get from where you got "div_code" and "open_hardware" my folder do not contain these files.

Are you using LINUX ? Or what path is that.

Thank you.
Reply
#8
(Aug-02-2020, 11:37 AM)samuelbachorik Wrote: If i have hardwaremonitor on my desktop in windows, my code will look like this ?
It's bettet to make own folders for this and not use Desktop for this as it has many other files.
Should be only folder with files that i explain and show.
(Aug-02-2020, 11:37 AM)samuelbachorik Wrote: I think i still dont get from where you got "div_code" and "open_hardware" my folder do not contain these files.
I have made those folder,div_code is just folder where i have most of my Python files and folders.
So i can put only files i want there.
Quote:Are you using LINUX ? Or what path is that.
No,some lacking in basic understanding of OS's and it's file structure.
Linux do not have root drive(C:\) in paths.
# Windows
C:\Users\Samuel\Desktop\

# The same on Linux would be
/home/Samuel/Desktop
I think you should learn some basic OS skill like making folder(mkdir),files(type),navigate(cd),list files(dir)...ect,
just using cmd tutorial and not file explorer,i do not like cmd so i use cmder(then can use Linux commands to).
Using command line is and import skill when programming,and this skill can not be skipped and hope for the best Wink
Reply
#9
Thank you a lot! python Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Tiny web server as standalone executable? Winfried 0 272 Feb-09-2024, 11:48 AM
Last Post: Winfried
  How to send data from a python application to an external application aditya_rajiv 1 2,131 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,447 Aug-30-2020, 08:14 AM
Last Post: glestwid
  Python 3.4: the only release to create .EXE standalone without .dll samsonite 7 5,733 Feb-28-2019, 09:20 AM
Last Post: samsonite
  Standalone interpreter? stf92 2 2,775 Mar-11-2018, 12:33 AM
Last Post: stf92
  Correct way to import from a standalone module in a different location chowder 3 18,309 Dec-16-2017, 07:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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