Workaround for Pyscript Importing Folder by Compiling Wheel
Why
Currently, we cannot import a folder of python package in pyscript, for example:
<py-env>
- paths:
- /myapp8763/__init__.py
- /myapp8763/funcs.py
</py-env>
This py-env do seems to import a python package, but it doesn't work. The below script will result in error.
<py-script>
import myapp8763.funcs # can't find package myapp8763
myapp8763.funcs.main()
</py-script>
The user can only import single file and use it like importing a single file.
<py-script>
import funcs
funcs.main()
</py-script>
But it breaks package.
Workaround
Pyscript provide another method: Importing a wheel:
<py-env>
- ./dist/myapp8763-0.0.1-py3-none-any.whl
</py-env>
If we want to import a folder, we should compile a wheel file.
Structure
.
├── index.html
├── myapp8763
│ ├── __init__.py
│ └── funcs.py
└── setup.py
Compile wheel
To compile a package as a wheel, we need setup.py
:
import setuptools
setuptools.setup(
name='myapp8763',
version='0.0.1',
packages=setuptools.find_packages('.'),
package_dir={
'myapp8763': 'myapp8763',
},
setup_requires=['wheel'],
)
After compiling wheel:
python3 setup.py build bdist_wheel
We can find wheel file is generate in dist folder:
├── dist
│ └── myapp8763-0.0.1-py3-none-any.whl
Include and Use
<py-env>
- ./dist/myapp8763-0.0.1-py3-none-any.whl
</py-env>
<py-script>
import myapp8763.funcs
</py-script>
Code
The whole code is here: https://github.com/mudream4869/pyscript-local-package