Using Python at the Martinos Center
Running Python at the Martinos Center
There are two ways to use Python at the Martinos Center. The first method is to completely install your own Python distribution using Miniforge. This is the only way to have complete control of the Python version used which may be a requirement for various versions of packages you want to install. The second mode is to use the venv module with the Python installed with the Linux OS. Note that using the Python that comes with the OS requires you to use explicity python2 or python3 at the command line. There is no default just python.
Installing a Custom Python for Yourself with Miniforge
Note that Anaconda/Miniconda is NOT FREE to install and use at MGB. See our Miniforge Migration page for details and for how to replace any Anaconda/Miniconda installs you have with Miniforge.
Install Miniforge by running
cd /tmp curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" bash Miniforge3-$(uname)-$(uname -m).sh
Make sure NOT to install the distribution into the default location in your home directory as your home directory quota is too small. Various things conda/pip do will also write lots of data to your ~/.conda, ~/.cache/pip and ~/.local/share/python* directories so you may need to symlink these to group storage volumes with more space.
Also I strongly suggest NOT having your ~/.cshrc or ~/.bashrc initialize your Miniforge install immediately on login. Instead setup a shell alias to do that on demand in each shell/Terminal when you need it. Minforge can put binaries in your PATH that can interfere with other Linux systems program or even prevent GUI login from working. NoMachine, VNC and OpenOffice are known to break when sourcing anaconda automatically depending on the packages installed in your environment.
For example, one would create an alias in their shell that ran:
eval "$(/vast/itgroup/miniforge3/bin/conda shell.bash hook)"
replacing the path to conda with where you installed your custom Miniforge and replacing “bash” with “tcsh” if you use the tcsh shell instead.
If you do let the installer modify your .bashrc/.tcshrc, another option is set “auto_activate_base: false” in your ~/.condarc file. Then you can run “conda activate base” later.
Using venv module with OS installed python
Using the venv module with the system Linux OS installed python is a simple and quick way to install modules with pip. If those package modules have complicated 3rd party library dependencies this may not work for you and you will be better off using Miniforge.
Here is an example of using this method to install jupyter:
cd /vast/itgroup/raines mkdir jupyter python3 -m venv /vast/itgroup/raines/jupyter source /vast/itgroup/raines/jupyter/bin/activate pip3 install --upgrade pip pip3 install jupyterlab /vast/itgroup/raines/jupyter/bin/jupyter lab --no-browser
Note that you will have to do the source line in other shells or in new logins. It is still best NOT to put this in your login scripts unless you know it will not interfere with other programs.
Resources for Learning Python
- Python Scientific Lecture Notes
- Using Python with MATLAB
- NumPy for Matlab Users
- The Boston Python User Group regularly holds meetings and organizes introductory Python classes
Neuroimaging Python Modules
The following neuroimaging related modules are ones many users will want to install in their Anaconda distribution:
- mne-python for M/EEG analysis
- nibabel provides access to a large number of neuro-imaging file formats
- nitime for time-series analysis <
- pydicom enables you to read, modify and write DICOM files with python code
- PySurfer for visualization and interaction with cortical surface representations of neuroimaging data from FreeSurfer (Requires using environment pysurfer in anaconda2.7)
- TensorFlow for machine learning (Requires using either the tensorflow or tensorflow-gpu environment in anaconda2.7)
Always prefer installing a module using the conda installer with a prebuilt package. If you cannot find one that then you can resort to building it with pip.
Run pip list to see a full list of modules
Miniforge is also a good way to install and run the R statistical software by installing the conda package r-essentials
Last Updated on December 3, 2024 by Paul Raines