I'm using pygattlib to interface with Bluetooth device in Python.
It works just fine, but the problem occurs when I try to use DBus in my code.
If I import gattlib and GLib.MainLoop().run(), the program freezes when calling run().
I found that gattlib has its own MainLoop for internal async calls.
I didn't find anything that would cover this specific issue, just some examples of having multiple mainloops in C.
This is the relevant part of the Python code:
import gattlib as bt
try:
from gi.repository import GLib
except ImportError:
import glib as GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
[...do stuff with gattlib here...]
DBusGMainLoop(set_as_default=True)
dbusService = SystemDBus()
try:
loop = GLib.MainLoop()
loop.run() # it stalls here, doesn't respond to DBus or anything
except KeyboardInterrupt:
GLib.MainLoop().quit()
Related
I have a problem freezing my tkinter application. As long as I don't import an external library the frozen app works. If I import a module like pandas the app crashes with no error messages. My OS is macOS monterey, I also tried on a windows machine and the same problem happens. Here's an example code:
from tkinter import *
import pandas as pd # it only works after building if this line is ommited
class MyApp(Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Welcome to My_App")
self.geometry('350x200')
app = MyApp()
app.mainloop()```
It seems there is a problem with the conda environment and there is somehow a clash between the conda env and cx_Freeze. It was solved when I created a pyenv.
import os
import re
import fitz # requires fitz, PyMuPDF
import pdfrw
import subprocess
import os.path
import sys
from PIL import Image
In my case fitz doesn't exist since it needs PyMuPDF. Is there a way to tell python to download dependencies and install them if they don't exist?
I am new to python and learning a lot. Apologizes in advance
Using Python 3.9.4
Platform that I am developing on is macOS but will be deploying on Windows
Editor is VSCode
Using try-catch to handle missing package
Ex:
import subprocess
def install(package):
subprocess.call(['pip', 'install', package])
try:
import fitz # requires fitz, PyMuPDF
except:
install('fitz')
A better practice would be to handle this before your code is executed. Example: using a requirements.txt file with all the dependent packages. And running the file before code execution.
I have a stupid question I think.
I have a script develloped on Windows but the goal is to run it on a Centos Server 7. The script work well on windows but not on Centos.
I have this error :
[root#114697 scripts]# python3.6 synch.py
Traceback (most recent call last):
File "synch.py", line 9, in <module>
from msvcrt import getch
ModuleNotFoundError: No module named 'msvcrt'
My script start with this :
from __future__ import division
import websocket
import thread
import time
import random
import sys
import json
import pymysql
import datetime
from time import ctime
from time import sleep
from msvcrt import getch
from pprint import pprint
import os
Seems msvcrt import getchcome from Microsoft ...
Can some one help to solve this problem please ?
Note : Python 3.6 is not involved, it was compiled in a clean way on the server.
Seems
Yoki
If you read the documentation for the msvcrt module, the first two sentences have this to say:
These functions provide access to some useful capabilities on Windows platforms. Some higher-level modules use these functions to build the Windows implementations of their services.
Since Centos Server 7 is not Windows, you will need to find an alternative approach to getch.
Solved ! I just remove
from msvcrt import getch
In fact in don't need to interact with the script on Centos ... it is a deamon :)
When I import winsound and then try to run the program, it returns an error message saying:
ImportError: No module named 'winsound'.
Are there any settings I need to change?
winsound only exists in Python installed under Windows. Do not attempt to import it if you are not running under Windows.
I'm lost in version 3.. in python2+gdk2 is:
#!/usr/bin/env python2
import gtk
gtk.gdk.window_process_all_updates()
window_xid = 54525964
gdk_window = gtk.gdk.window_foreign_new(window_xid)
which is pretty much straight forward. But then, the horror:
#!/usr/bin/env python3
from gi.repository import xlib
from gi.repository import Gdk
from gi.repository import GdkX11
Gdk.Window.process_all_updates()
xlib_window = "???????"
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
the xlib is killing me.. I'm unable to do anything with it. Has anybody worked with it before??
The documentation I've through several times already is:
Gdk3,
Xlib
Getting the window from its xid was the fastest way to get a screenshot in python2 I guess I'll have try another way in python3.. any ideas? maybe peek_children from root window?? but then, how do I know if it's the window I want?
A Window in X11 is the same as an XID. There's just a typedef from one to the other.
So in C code gdk_x11_window_foreign_new_for_display() just accepts an Window or XID, which is basically an integer. This also works in python using introspection:
#!/usr/bin/env python3
from gi.repository import Gdk
from gi.repository import GdkX11
Gdk.Window.process_all_updates()
xlib_window = 0x2a00005 # from xwininfo command
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
print(gdk_window.get_geometry())