zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
8.72k stars 530 forks source link

Runtime Error when trying to run native=True #1841

Open swrpug opened 10 months ago

swrpug commented 10 months ago

Description

I'm getting a Fork vs Spawn error when attempting to run a native app on Ubuntu.

Sample Application (from documentation)

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Invoked from command line from within a virtual environment: python main.py

Trackback

Traceback (most recent call last):
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 15, in <module>
    main()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/main.py", line 11, in main
    ui.run(native=True, window_size=(400, 300), fullscreen=False)
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/nicegui/run.py", line 180, in run
    ChangeReload(config, target=globals.server.run, sockets=[sock]).run()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 49, in run
    self.startup()
  File "/mnt/ext3tbdrive/sandbox/try-nicegui/env/lib/python3.11/site-packages/uvicorn/supervisors/basereload.py", line 85, in startup
    self.process.start()
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/home/tim/.pyenv/versions/3.11.6/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

OS: Ubuntu 20.04 Python: 3.11.6 Pyenv: 2.3.29-5-g62e2e9c7 nicegui: 1.3.17 pywebview: 4.3.3

rodja commented 10 months ago

Strange. On Mac it's working fine. @JensOgorek could you check if the error is reproducible on your system?

JensOgorek commented 10 months ago

@rodja I just saw this. Can confirm an error. It starts normally, but clicking on enlarge causes connection lost at nicegui version 1.3.17 and on 1.4.1.

muratbedir commented 10 months ago

On the Windows 11, it requires webview to run

image

microsoft code log

PowerShell 7.3.8
PS C:\Users\mbedir\Desktop\niceekran>  & 'C:\Program Files\Python311\python.exe' 'c:\Users\mbedir\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '61418' '--' 'C:\Users\mbedir\Desktop\niceekran\destopapp.py'
PS C:\Users\mbedir\Desktop\niceekran>  c:; cd 'c:\Users\mbedir\Desktop\niceekran'; & 'C:\Program Files\Python311\python.exe' 'c:\Users\mbedir\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '61442' '--' 'C:\Users\mbedir\Desktop\niceekran\destopapp.py'

Native mode is not supported in this configuration.
Please run "pip install pywebview" to use it.
PS C:\Users\mbedir\Desktop\niceekran> pip install pywebview  
Defaulting to user installation because normal site-packages is not writeable
DEPRECATION: Loading egg at c:\program files\python311\lib\site-packages\vboxapi-1.0-py3.11.egg is deprecated. pip 23.3 will enforce this behaviour change. A possible replacement is to use pip for package installation..
Collecting pywebview
  Obtaining dependency information for pywebview from https://files.pythonhosted.org/packages/94/eb/fec1105c0e3b459266637bdc867bd064619eddb170dc0c9d12f20ad7690c/pywebview-4.4.1-py3-none-any.whl.metadata
  Downloading pywebview-4.4.1-py3-none-any.whl.metadata (4.0 kB)
Collecting proxy-tools (from pywebview)
  Downloading proxy_tools-0.1.0.tar.gz (3.0 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: bottle in c:\users\mbedir\appdata\roaming\python\python311\site-packages (from pywebview) (0.12.23)
Requirement already satisfied: typing-extensions in c:\users\mbedir\appdata\roaming\python\python311\site-packages (from pywebview) (4.8.0)
Collecting pythonnet (from pywebview)
  Obtaining dependency information for pythonnet from https://files.pythonhosted.org/packages/12/28/2ecca48b313c436eb0b194b74b5a70dd88f2700d79bce5424799ffc82ede/pythonnet-3.0.3-py3-none-any.whl.metadata
  Downloading pythonnet-3.0.3-py3-none-any.whl.metadata (6.6 kB)
Collecting clr-loader<0.3.0,>=0.2.6 (from pythonnet->pywebview)
  Obtaining dependency information for clr-loader<0.3.0,>=0.2.6 from https://files.pythonhosted.org/packages/b3/1a/2801c4e3888aac8fcd4d88d32a1155a778d1c01ca040ae8137d433a39cc6/clr_loader-0.2.6-py3-none-any.whl.metadata
  Downloading clr_loader-0.2.6-py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: cffi>=1.13 in c:\users\mbedir\appdata\roaming\python\python311\site-packages (from clr-loader<0.3.0,>=0.2.6->pythonnet->pywebview) (1.15.1)
Requirement already satisfied: pycparser in c:\users\mbedir\appdata\roaming\python\python311\site-packages (from cffi>=1.13->clr-loader<0.3.0,>=0.2.6->pythonnet->pywebview) (2.21)
Downloading pywebview-4.4.1-py3-none-any.whl (405 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 405.9/405.9 kB 1.9 MB/s eta 0:00:00
Downloading pythonnet-3.0.3-py3-none-any.whl (290 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 291.0/291.0 kB 3.0 MB/s eta 0:00:00
Downloading clr_loader-0.2.6-py3-none-any.whl (51 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 51.3/51.3 kB 1.3 MB/s eta 0:00:00
Building wheels for collected packages: proxy-tools
  Building wheel for proxy-tools (setup.py) ... done
  Created wheel for proxy-tools: filename=proxy_tools-0.1.0-py3-none-any.whl size=2913 sha256=8b3a53f5a99ad97b8c2394db2a73ef5523255a24e849ee13b0db7091d9012d90
  Stored in directory: c:\users\mbedir\appdata\local\pip\cache\wheels\2b\5f\96\7e96e7cbf06e426e168a6891a6ca2226787cc79fc816830dd4
Successfully built proxy-tools
Installing collected packages: proxy-tools, clr-loader, pythonnet, pywebview
Successfully installed clr-loader-0.2.6 proxy-tools-0.1.0 pythonnet-3.0.3 pywebview-4.4.1

[notice] A new release of pip is available: 23.2.1 -> 23.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
PS C:\Users\mbedir\Desktop\niceekran>  c:; cd 'c:\Users\mbedir\Desktop\niceekran'; & 'C:\Program Files\Python311\python.exe' 'c:\Users\mbedir\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '61479' '--' 'C:\Users\mbedir\Desktop\niceekran\destopapp.py'

0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
NiceGUI ready to go on http://localhost:8000 (install netifaces to show all IPs and speedup this message)
[pywebview] Using WinForms / Chromium
PS C:\Users\mbedir\Desktop\niceekran>  c:; cd 'c:\Users\mbedir\Desktop\niceekran'; & 'C:\Program Files\Python311\python.exe' 'c:\Users\mbedir\.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\liions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '6154ions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '61543' '--' 'C:\Users\mbedir\Desktop\niceekran\destopapp.py'
0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
NiceGUI ready to go on http://localhost:8000 (install netifaces to show all IPs and speedup this message)
[pywebview] Using WinForms / Chromium
rodja commented 10 months ago
Native mode is not supported in this configuration.
Please run "pip install pywebview" to use it.

@muratbedir Yes the message is expected because we made pywebview an optional dependency. It has nothing to do with this issue.

fdawnxl commented 10 months ago

I also encountered the same situation.Environment related information is as follows:

When I execute the following code in the venv virtual environment, python will report an error:

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)
Traceback(part):
  File "/usr/lib/python3.11/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "/usr/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

Because pip cannot be used to directly install python packages under ArchLinux (it will conflict with pacman), and there is no packaged python-nicegui package, so I have been developing in a virtual environment. I used to think that the error was due to executing NiceGUI in a virtual environment, but after reading this article, I tend to think that it is due to incorrect configuration of multiprocessing.(However, I still recommend that you directly use the system python environment to execute the NiceGUI code in a Linux distribution that can use pip to determine whether this problem can be reproduced.)

dev-robasi commented 7 months ago

It works in windows via a virtual env. Had same error with wsl-ubuntu in a virtual env.

nimmadev commented 7 months ago

same issue on system: Linux 6.5.13-7-MANJARO python: 3.12.1 python installed with pyenv using virtual env

ahdeddd commented 7 months ago

Same issue here on Manjaro Linux. I installed pywebview with --break-system-packages

yuanxion commented 7 months ago

1."RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context." issue is not happened when running the following sample on my host with Python 3.10 and nicegui (both 1.3.17 & 1.4.12).

// On host Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 6.5.0-14-generic Python: 3.10.13 nicegui: 1.3.17/1.4.12 pywebview: 4.4.1

from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

2.But I will have another issue when running nicegui with "native=True": Connection terminated unexpectedly, as described https://github.com/zauberzeug/nicegui/issues/2419.

electronstudio commented 6 months ago

Add to the top of your code:

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

On Linux I also had to pip install qtpy pyqt5 PyQtWebEngine

fdawnxl commented 6 months ago

I've tried the method above, but NicuGUI still throws the same error as before. I think this might be a venv issue. So I created an ubuntu virtual machine (with Gnome desktop) and installed NiceGUI and pywebview in the system python environment. After I installed the relevant dependencies of pywebview, I directly executed the sample code on the homepage. Surprisingly, the program was executed successfully:

Screenshot from 2024-02-15 00-01-24

Since I did not use the same system and kernel as the host machine(Arch Linux, linux-zen) when I created the virtual machine(Ubuntu, linux), I cannot find the problem by controlling variables for the time being. However, this may indicate that there is no problem with the code of NiceGUI itself. By the way, when using pywebview, you may get an error due to missing dependencies. You can refer to this URL to solve this problem.

initiateit commented 6 months ago

Using:

from nicegui import app, ui
import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Getting Error:

RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

Operating system is: Ubuntu 23.10

Installed via: python3 -m pip install pywebview nicegui qtpy pyqt5 PyQtWebEngine

@rodja sorry I somehow forgot to copy that into my post.

Aikufurr commented 4 months ago

Getting same issue

Host: Operating System: EndeavourOS x86_64 Kernel: 6.8.7-zen1-1-zen Python: 3.11.8 nicegui: 1.4.15 pywebview: 5.1

gsal commented 3 months ago

Getting same issue:

Rocky 9.3 Python 3.11.9 Nicegui 1.4.25 pywebview 5.1

BUT, I cannot install what I need into the system, so, I am working out of a conda environment.

StephanieSunshine commented 2 months ago

Same issue EndeavourOS x86_64

pywebview: 5.1 nicegui: 1.4.26 python: 3.12.3

Tried to install python-pywebview from AUR to possibly bring in any dependencies missing. Still no luck. AUR

knoppmyth commented 2 months ago

Here is how I got it working using venv (Using Arch Linux):

python -m venv venv
source venv/bin/activate
pip install nicegui pywebview qtpy pyqt5 PyQtWebEngine

Here is the code I was able to run without issue.

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)
from nicegui import app, ui

app.native.window_args['resizable'] = False
app.native.start_args['debug'] = True

ui.label('app running in native mode')
ui.button('enlarge', on_click=lambda: app.native.main_window.resize(1000, 700))

ui.run(native=True, window_size=(400, 300), fullscreen=False)

Note: I only saw the error other reported above if:

import multiprocessing
multiprocessing.set_start_method("spawn", force=True)

Came after:

from nicegui import app, ui