Skip to content

Python Woes

Rudolph Pienaar edited this page Feb 2, 2021 · 2 revisions
Caution
This page is mostly unrelated to cookiecutter-chrisapp! You should be debugging your ChRIS plugin in a container. See our Developer’s Guide

Python Environments

Virtual environments enable you to have different versions of the same package installed on the same system.

You need to specify a location for your environment (where packages are installed to). For example, ~/chrisenv

There are three popular solutions, just pick one and stick with it:

Warning
if you are working on a machine at the FNNDSC, you might encounter this bug.

venv

venv comes with Python 3, nothing extra to install.

# set up, run once
python -m venv ~/chrisenv
# activate, run each time you open a shell
source ~/chrisenv/bin/activate
# (optional) deactivate, run after you're done working
deactivate

virtualenv

Install virtualenv and virtualenvwrapper

pip install virtualenv virtualenvwrapper

Setup your virtual environments

Create a directory for your virtual environments e.g

mkdir ~/python_envs

Open your .bashrc file

vim ~/.bashrc

Add these two lines to your .bashrc file

export WORKON_HOME=~/python_envs
source $(which virtualenvwrapper.sh | head -n 1)

Create a new Python3 virtual environment

mkvirtualenv --python=python3 chrisapp_env

To activate chrisapp_env

workon chrisapp_env

To deactivate chrisapp_env

deactivate

conda

Conda is general-purpose, it is not specific to Python.

Note
anaconda users on macOS should use the conda environment tools directly instead. See this thread and the linked discussion on the anaconda mailing list. A working set of commands is noted here.

Long Shebangs (Bug)

pip changes the shebang (#!/bin/python) of scripts installed to the virtualenvironment.

When the virtualenvironment’s realpath is unusually long, it will not work. E.g. the FNNDSC LDAP mount point causes the shebang to be #!/net/rc-fs-nfs/ifs/data/NoSync/FNNDSC-NR/neuro/labs/grantlab/users/my.name/my.virtualenvironment/bin/python.

The error will be:

-bash: /net/rc-fs-nfs/ifs/data/NoSync/FNNDSC-NR/neuro/labs/grantlab/users/.../bin/python: bad interpreter: No such file or directory

Fix

Replace the shebang with #!/usr/bin/env python.

sed -i '1 s/\/.*\//\/usr\/bin\/env /' file_to_patch.py