Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing zipapp with Python 3.10 or earlier fails on importing 'tomli' #1142

Closed
sgryjp opened this issue Dec 12, 2023 · 3 comments · Fixed by #1194
Closed

Executing zipapp with Python 3.10 or earlier fails on importing 'tomli' #1142

sgryjp opened this issue Dec 12, 2023 · 3 comments · Fixed by #1194
Labels
bug Something isn't working zipapp

Comments

@sgryjp
Copy link
Contributor

sgryjp commented Dec 12, 2023

Describe the bug

Executing the zipapp of pipx with Python 3.10 or earlier fails on trying to import tomli which is not included in pipx.pyz.

How to reproduce

Executing pipx.pyz of version 1.3.2 using Python 3.10 or earlier will always reproduce the error. Below is an example output:

mac:~/Downloads% python3.11 pipx.pyz --version
1.3.3
mac:~/Downloads% python3.10 pipx.pyz --version
Traceback (most recent call last):
  File "/Users/sgryjp/Downloads/pipx.pyz/_bootstrap/__init__.py", line 76, in import_string
  File "/Users/sgryjp/.shiv/pipx.pyz_0fe43a768b3b979cecbbd4b3678299380e71ea146670c0f28fd6199751c78f96/site-packages/pipx/main.py", line 23, in <module>
    from pipx import commands, constants
  File "/Users/sgryjp/.shiv/pipx.pyz_0fe43a768b3b979cecbbd4b3678299380e71ea146670c0f28fd6199751c78f96/site-packages/pipx/commands/__init__.py", line 7, in <module>
    from pipx.commands.run import run
  File "/Users/sgryjp/.shiv/pipx.pyz_0fe43a768b3b979cecbbd4b3678299380e71ea146670c0f28fd6199751c78f96/site-packages/pipx/commands/run.py", line 30, in <module>
    import tomli as tomllib
ModuleNotFoundError: No module named 'tomli'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sgryjp/Downloads/pipx.pyz/_bootstrap/__init__.py", line 84, in import_string
AttributeError: module 'pipx' has no attribute 'main'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/homebrew/Cellar/python@3.10/3.10.13_1/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/sgryjp/Downloads/pipx.pyz/__main__.py", line 3, in <module>
  File "/Users/sgryjp/Downloads/pipx.pyz/_bootstrap/__init__.py", line 253, in bootstrap
  File "/Users/sgryjp/Downloads/pipx.pyz/_bootstrap/__init__.py", line 81, in import_string
  File "/Users/sgryjp/Downloads/pipx.pyz/_bootstrap/__init__.py", line 87, in import_string
ImportError: module 'pipx' has no attribute 'main'

Expected behavior

pipx.pyz can be executed without an error.

@sgryjp
Copy link
Contributor Author

sgryjp commented Dec 12, 2023

It seems that zipapp file is created by invoking shiv using Python 3.12. Since tomli is excluded from dependencies for Python 3.11 or newer in pyproject.toml, that package is also excluded from the zipapp I guess?

@jradxl
Copy link

jradxl commented Jan 4, 2024

I've been puzzling over this for some hours, and glad I searched for before posting a separate bug.
On Ubuntu 22.04 LTS, the "apt install pipx" results in v1.0.0, which is clearly out of date, but results in pipx being installed for the root user. In my inexperience I downloaded your pipx.pyz and found the issue of the missing tomli when run as root.
I see now why you recommend, python3 -m pip install --user pipx but of course one gets the pip warning message when run as root.

Isn't it possible for you to release a version compatible with 22.04, which is an LTS release?

How to you create the pipx.pyz release file? I don't see anything in the project that says a zipapp is created, but clearly after reverse engineering you use Shiv?

@sgryjp
Copy link
Contributor Author

sgryjp commented Jan 6, 2024

How to you create the pipx.pyz release file? I don't see anything in the project that says a zipapp is created, but clearly after reverse engineering you use Shiv?

@jradxl As far as I see, the zipapp file pipx.pyz is built in a procedure below:

  • For every commit (push), a zipapp is built in the zipapp job of tests workflow in GitHub Action.
    • (side-note: Uploading the file to GitHub Release is also done in the upload-zipapp task in the same workflow, which will be triggered only when a Git tag was pushed.)
  • The zipapp job just executes nox specifying with an option --session zipapp so the actual procedure is zipapp function defined in noxfile.py.
  • Since python option is not given to the session decorator for the zipapp function, it should be executed using the default version of Python.
    • I believe it is 3.12

I confirmed that executing the zipapp session using Python 3.8 creates a zipapp file which contains tomli package and also can be executed with Python 3.8 through 3.12. I'll make a PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working zipapp
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants