question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The "create a pyz of shiv using shiv" example doesn't work

See original GitHub issue

As written, the example leaves the shiv pyz file with a shebang pointing at the (presumably temporary) virtual environment used to build it.

The example should probably include a -p "/usr/bin/env python3" argument to specify an explicit interpreter.

Actually, given that shiv pyz files are self-contained, there’s no real benefit to them ever having shebangs that refer to a virtualenv - maybe it would be worth making the default shebang /usr/bin/env python3, or at least locate the “base” Python interpreter for a virtualenv - which can be done as

Path(getattr(sys, 'real_prefix', sys.base_prefix)) / Path(sys.executable).relative_to(sys.prefix)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
pfmoorecommented, Oct 25, 2018

Yeah, that looks really good. Tested on Windows using virtualenv and venv and it seems fine.

1reaction
pfmoorecommented, Oct 25, 2018

Ah, yes. My mistake - I didn’t test it properly.

The problem is that you need to get from the virtualenv/venv back to the original base installation. That’s what sys.base_prefix gives you (for a venv) and sys.real_prefix (for a virtualenv). So all the getattr stuff is covering both bases there.

But they give you the equivalent of sys.prefix, not sys.executable. And the executable gets put in various places - what I was trying to do was avoid hard coding the fact that it’s in Scripts/python.exe on Windows, and bin/python on Unix. But that’s for virtual environments, and the whole point here is to get a non-virtual location. So I was being dumb. What you actually need is just

Path(getattr(sys, 'real_prefix', sys.base_prefix)) / 'python.exe`

That’s for Windows - of course, you’ll need to miss of the .exe suffix on Unix, and maybe do something about python2 vs python3 - and maybe Python isn’t even in sys.prefix, but in /usr/bin or /usr/bin/local…? Or get everyone to switch to Windows where it’s simple 😉

The more I think about it, the more I’d go for #!/usr/bin/env python3 (use “python3” or “python2” depending on sys.version_info). The Windows launcher handles that, and (hopefully!) it’s reliable on Unix.

Or maybe even do what zipapp does, and default to no shebang, forcing the user to run the pyz as python xxx.pyz or be explicit about what they want via -p.

Read more comments on GitHub >

github_iconTop Results From Across the Web

shiv — shiv documentation
Shiv is a command line utility for building fully self-contained Python zipapps as outlined in PEP 441 but with all their dependencies included!...
Read more >
Creating Python app using shiv - Medium
Shiv creates a zipped file .pyz with all the needed dependencies packaged inside. The .pyz file can be then transported to other machine...
Read more >
Dissecting a Python Zipapp Built with Shiv | Lincoln Loop
Enter Shiv. This downloads awscli and all its dependencies from PyPI and creates a zipapp ( aws. pyz ) which will run a...
Read more >
Packaging - Discussions on Python.org
Thinking about Pip plans to introduce an alternative (zipapp) deployment method and how that may lead to Provide a `py run`/`py z` ...
Read more >
shiv - PyPI
You can even create a pyz of shiv using shiv! python3 -m venv . source bin/activate pip install shiv shiv -c shiv -o...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found