erikbstack
erikbstack

Reputation: 13254

Call either system's Python or virtualenv's Python in a Python script

Suppose you write a Python shell script. It will probably start with something like this:

#!/usr/bin/python

The problem is, if you often work with virtualenv this call is just plain wrong. You actually would like this script to call virtualenv's python binary, if it is in this environment and /usr/bin/python/ otherwise. Just like your shell would decide, when you write python as a shell command.

How would you write your #! line to fulfil this requirement?

Upvotes: 1

Views: 82

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1122412

Use #!/usr/bin/env python instead.

The env command looks up binaries in the current PATH; activating your virtual environment adds your virtualenv bin/ directory to the path and env will find your python binary there instead of the global python.

Upvotes: 6

freestyler
freestyler

Reputation: 5444

try:

#!/usr/bin/env python

...

Upvotes: 0

Related Questions