Meta: this "how do I manage multiple Pythons?" thing has come up a couple
of times lately; are people interested in a FAQ section?

On 23 October 2016 at 03:54, Eugene Yunak <[email protected]> wrote:
> I'd set the shebang to `/usr/bin/env python3`, or `/usr/bin/env python`
if you
> do not care whether 2 or 3 would be used.

Use `virtualenv' (you may need to install it separately; `pip install
virtualenv')
if you need to use a mix of Python versions, and always use the latter
shebang
form.

So, an example. Say you found two Python apps that you want to use.
Let's call them "oldapp" and "newapp". oldapp needs python 2.7 and
newapp is OK with python 3+. Both include 'requirements.txt' files to
indicate what their Python package dependencies are.

If it's a popular app you may be able to install the app this way as well,
with the `pip' utility, thus keeping it all nicely contained within the
virtualenv. But I'll assume that that's not possible here.

The apps are installed in $HOME/apps/oldapp and $HOME/apps/newapp

1. After installing both Pythons, make a place to keep your virtualenvs

mkdir $HOME/py

2. Create the virtualenvs and install dependencies

virtualenv -p /usr/local/bin/python2.7 $HOME/py/oldapp
. $HOME/py/newapp/bin/activate
python --version ### to demonstrate that virtualenv works
pip install -r $HOME/apps/oldapp/requirements.txt

virtualenv -p /usr/local/bin/python3 $HOME/py/newapp
. $HOME/py/newapp/bin/activate
python --version ### to demonstrate that virtualenv works
pip install -r $HOME/apps/newapp/etc/requirements.txt

3. Observe that all of these dependencies are installed inside the
relevant virtualenv. So they will never conflict with each other or
pollute your /usr/local tree.

4. To actually run an app

. $HOME/py/newapp/bin/activate
$HOME/apps/newapp/bin/newapp.py

. $HOME/py/oldapp/bin/activate
$HOME/apps/oldapp/bin/oldapp.py

Hope this helps.

John

Reply via email to