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.

Environment activation on the shebang line?

See original GitHub issue

I was inspired to ask about specifying the project dir as a command line arg (https://github.com/pypa/pipenv/issues/2237) while thinking about invoking tools in their own venvs. At some point I realized that I could use a venv’s python to invoke a tool and it would just work without having to activate anything in my shell. I assume this is entirely by design.

With pipenv managing venv paths, it might be compelling to be able to invoke a tool via pipenv, where pipenv would determine the venv from the tool’s path and not the current working directory. Perhaps pipenv could be the tool runner on the shebang line, something like this:

#!/usr/bin/env pipenv run

import mystuff
import sys

def main(args):
  # And so forth.
  return 0

if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))

Is this compelling? Or am I missing a different best practice for organizing tools with their own venvs? Or do people just prefer to not do this and use global site-packages for everything they intend to run on their own machines? (And if I’m dev’ing my own tools, I would do actual releases into my everyday-carry environment, and use venvs just for development?)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
cfzlpcommented, Nov 16, 2018

This should do it (works for me):

#!/usr/bin/env pipenv run python

8reactions
uranusjrcommented, Aug 27, 2018

This is an issue, but not one Pipenv wishes to get into. Pipenv is a tool to manage environments, not to help package and redistribute your application. You should use other tools for that purpose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I activate a conda env in a subshell? - Stack Overflow
Instead of activating the environment in the subshell, it just says, go ahead and ignore the shebang... just use the $ python program.py...
Read more >
Make Linux/Unix Script Portable With #!/usr/bin/env ... - nixCraft
Learn how to make Linux/Unix script portable with #!/usr/bin/env shebang to execute script using the interpreter such as bash, perl, python.
Read more >
PEP 394: Clarify Bullet for #!/usr/bin/env python
For scripts that are only expected to be run in an activated virtual environment, shebang lines can be written as #!/usr/bin/env python, ...
Read more >
28.3. venv — Creation of virtual environments
There should be no need in other circumstances to activate a virtual environment—scripts installed into virtual environments have a “shebang” line which ...
Read more >
activate a virtual environment from within shell script
There should be no need in other circumstances to activate a virtual environment; scripts installed into virtual environments have a “shebang” line which ......
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