Python environments - theunissenlab/lab-documentation GitHub Wiki
We are currently transitioning from Python 2.7 to Python 3.5. Most lab projects (e.g. soundsig, zeebeez, songephys) are not yet Python 3 compatible, but we are working on it. For now, use Python 2.7 but try to write Python 3 compatible code.
The two preferred ways to manage Python packages are with anaconda (miniconda), or with virtualenv.
Setup
Python 2 and Python 3 come pre-installed on new versions of Ubuntu and Mac, and should already be on your workstations. If you choose to use anaconda or anaconda3 (Python 3 version), it will modify your bashrc so that you use an installation of Python that comes with anaconda (rather than your default system version).
Creating an environment
anaconda
conda create ENV_NAME
virtualenv
-
In the root directory of each project you work on, type
virtualenv env --python [python|python3]to create a new environment inenv/ -
Activate the environment by running
source env/bin/activate. This modifies your PATH to point to the virtualenv packages. Do this every time you need to install any packages or run any code, including starting a jupyter notebook.
Installing packages
anaconda
conda install PKG
virtualenv
pip install PKG
Packages installed with pip with a virtualenv activated are installed in env/ and cannot be seen outside the env. Similarly, packages installed with pip outside the virtualenv cannot be seen inside the virtualenv.
To start with a blank slate, just delete the env/ directory and create a new environment.
Defining project dependencies in requirements.txt
Dependencies can be defined in a "requirements" file usually called requirements.txt. Typically, it lists pypi packages with version numbers in the form PKG==VERSION. It can also include references to git repositories and commit hashes to install from a repository.
Install from a requirements file:
pip install -r requirements.txt
Python 2 to 3 migration
Here are some of the common issues you may hit when trying to move from Python 2 to 3
The print statement print "hello" has been replaced with the print function print("hello"). Maintain 2 and 3 compatibility by adding from __future__ import print_function and using the new syntax.
division
The division operator / will do floating point division by default. What used to be 7 / 2 == 3 will now be 7 / 2 == 3.5. In Python 3, integer (floor) division can be explicitly done with the // operator, i.e. 7 // 2 == 3. Maintain 2 and 3 compatibility by replacing any instance of integer division with // and adding from __future__ import division.
dictionary items
The old dict.items() would return a list of tuples of (key, value) pairs. In python 3, it will return a dict_items iterable. Iteration as for key, value in d.items() will still work, but other things may break...?
xrange
xrange used to be a iterator version of range. Now, range is an iterator by default and xrange no longer exists. Just replace all usages of xrange with range.