DOC Add FAQ entry on detecting pyodide at run and build time (#790)

This commit is contained in:
Roman Yurchak 2020-11-07 12:21:54 +01:00 committed by GitHub
parent 7d3a6da40c
commit 7d4fbe9d1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -15,6 +15,8 @@ export HOSTPYTHON=$(HOSTPYTHONROOT)/bin/python3
export TARGETPYTHONROOT=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION) export TARGETPYTHONROOT=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION)
export PYTHONINCLUDE=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION)/include/python$(PYMINOR) export PYTHONINCLUDE=$(PYODIDE_ROOT)/cpython/installs/python-$(PYVERSION)/include/python$(PYMINOR)
# This env variable is used to detect pyodide at build time,
# do not rename it.
export PYODIDE_PACKAGE_ABI=1 export PYODIDE_PACKAGE_ABI=1
export SIDE_LDFLAGS=\ export SIDE_LDFLAGS=\

View File

@ -18,3 +18,35 @@ In both cases, files need to be served with a web server and cannot be loaded fr
For security reasons JavaScript in the browser is not allowed to load local For security reasons JavaScript in the browser is not allowed to load local
data files. You need to serve them with a web-browser. data files. You need to serve them with a web-browser.
## How to detect that code is run with Pyodide?
**At run time**, you can detect that a code is running with Pyodide using,
```py
import sys
if "pyodide" in sys.modules:
# running in Pyodide
```
More generally you can detect Python built with Emscripten (which includes
Pyodide) with,
```py
import platform
if platform.system() == 'Emscripten':
# running in Pyodide or other Emscripten based build
```
This however will not work at build time (i.e. in a `setup.py`) due to the way
the pyodide build system works. It first compiles packages with the host compiler
(e.g. gcc) and then re-runs the compilation commands with emsdk. So the `setup.py` is
never run inside the Pyodide environement.
To detect pyodide, **at build time** use,
```python
import os
if "PYODIDE_PACKAGE_ABI" in os.environ:
# building for Pyodide
```