Description
I don't know if this is a valid use case or out of scope for this project but since it's possible to install in another environment one might expect that it is also possible to compile bytecode for the target environment.
In the following example installer
lives in a Python 3.9 environment and installs tomli into a Python 3.11 environment. So far so good. However, it compiles bytecode for its own (Python 3.9) environment instead of the target (Python 3.11) environment. (Probably, it's not even possible to compile for the target environment without creating a subprocess to run the target interpreter?)
This issue was originally posted as a side note in python-poetry/poetry#7639. Before implementing our own solution, I just wanted to know if this will probably remain a known limitation of installer or if anyone has an idea if/how it could be supported by installer itself.
$ virtualenv -p 3.11 .venv11
$ virtualenv -p 3.9 .venv9
$ .venv9/bin/python -m pip install installer
$ .venv9/bin/python install_and_compile.py
$ ls .venv11/lib/python3.11/site-packages/tomli/__pycache__
__init__.cpython-39.pyc _parser.cpython-39.pyc _re.cpython-39.pyc _types.cpython-39.pyc
install_and_compile.py
import json
import subprocess
from installer import install
from installer.destinations import SchemeDictionaryDestination
from installer.sources import WheelFile
scheme_dict_oneliner = (
"import sysconfig; import json; print(json.dumps(sysconfig.get_paths()))"
)
scheme_dict = json.loads(
subprocess.check_output([".venv11/bin/python", "-c", scheme_dict_oneliner]).decode()
)
destination = SchemeDictionaryDestination(
scheme_dict,
interpreter=".venv11/bin/python",
script_kind="posix",
bytecode_optimization_levels=(0,)
)
with WheelFile.open("tomli-2.0.1-py3-none-any.whl") as source:
install(
source=source,
destination=destination,
additional_metadata={
"INSTALLER": b"amazing-installer 0.1.0",
},
)