SciPy

Development environment quickstart guide (macOS)

This quickstart guide will cover:

  • setting up and maintaining a development environment, including installing compilers and SciPy build dependencies

  • creating a personal fork of the SciPy repository on GitHub

  • using git to manage a local repository with development branches

  • performing an in-place build of SciPy

  • creating a virtual environment that adds this development version of SciPy to the Python path

in macOS.

Its companion videos Anaconda SciPy Dev: Part I (macOS) and Anaconda SciPy Dev: Part II (macOS) show many of the steps being performed. This guide may diverge slightly from the videos over time, with the goal of keeping this guide the simplest, up-to-date procedure.

Note

This guide does not present the only way to set up a development environment; there are many valid choices of Python distribution, C/Fortran compiler, and installation options. The steps here can often be adapted for other choices, but we cannot provide documentation tailored for them.

This guide assumes that you are starting without an existing Python 3 installation. If you already have Python 3, you might want to uninstall it first to avoid ambiguity over which Python version is being used at the command line.

Building SciPy

Consider following along with the companion video Anaconda SciPy Dev: Part I (macOS)

  1. Download, install, and test the latest release of the Anaconda Distribution of Python. In addition to the latest version of Python 3, the Anaconda distribution includes dozens of the most popular Python packages for scientific computing, the Spyder integrated development environment (IDE), the conda package manager, and tools for managing virtual environments.

  2. Install Apple Developer Tools. An easy way to do this is to open a terminal window, enter the command xcode-select --install, and follow the prompts. Apple Developer Tools includes git, the software we need to download and manage the SciPy source code.

  3. Browse to the SciPy repository on GitHub and create your own fork. You’ll need to create a GitHub account if you don’t already have one.

  4. Browse to your fork. Your fork will have a URL like https://github.com/mdhaber/scipy, except with your GitHub username in place of “mdhaber”.

  5. Click on the big, green “Clone or download” button, and copy the “.git” URL to the clipboard. The URL will be the same as your fork’s URL, except it will end in “.git”.

  6. Create a folder for the SciPy source code in a convenient place on your computer. Navigate to it in the terminal.

  7. Enter the command git clone followed by your fork’s .git URL. Note that this creates in the terminal’s working directory a scipy folder containing the SciPy source code.

  8. In the terminal, navigate to the scipy root directory (e.g., cd scipy).

  9. Install Homebrew. Enter into the terminal
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    or follow the installation instructions listed on the Homebrew website. Homebrew is a package manager for macOS that will help you download gcc, the software we will use to compile C, C++, and Fortran code included in SciPy.

  10. Use Homebrew to install gcc by entering the command brew install gcc.

  11. In the terminal, ensure that all of SciPy’s build dependencies are up to date: conda install pybind11, then conda update cython numpy pytest pybind11.

  12. (Optional) Check your present working directory by entering pwd at the terminal. You should be in the root /scipy directory, not in a directory ending /scipy/scipy.

  13. Do an in-place build: enter python3 setup.py build_ext --inplace.
    This will compile the C, C++, and Fortran code that comes with SciPy. We installed python3 with Anaconda. setup.py is a script in the root directory of SciPy, which is why you have to be in the SciPy root directory to call it. build_ext is a command defined in setup.py, and --inplace is an option we’ll use to ensure that the compiling happens in the SciPy directory you already have rather than the default location for Python packages. By building in-place, you avoid having to re-build SciPy before you can test changes to the Python code.

  14. Test the build: enter python3 runtests.py -v. runtests.py is another script in the SciPy root directory. It runs a suite of tests that make sure SciPy is working as it should, and -v activates the --verbose option to show all the test output.

If the tests were successful, you now have a working development build of SciPy! You could stop here, but you would only be able to use this development build from within the SciPy root directory. This would be inconvenient, for instance, if you wrote a script that performs an import of something you changed in SciPy but wanted to save it elsewhere on your computer. Without taking additional steps to add this version of SciPy to the PYTHONPATH , this script would import from the version of SciPy distributed with Anaconda rather than the development version you just built. (See here for much more information about how Python imports modules.)

Installing SciPy

Consider following along with the companion video Anaconda SciPy Dev: Part II (macOS)

Currently we have two versions of SciPy: the latest release as installed by Anaconda, and the development version we just built. Ideally, we’d like to be able to switch between the two as needed. Virtual environments can do just that. With a few keystrokes in the terminal or even the click of an icon, we can enable or disable our development version. Let’s set that up.

  1. In a terminal window, enter conda list.
    This shows a list of all the Python packages that came with the Anaconda distribution of Python. Note the latest released version of SciPy is among them; this is not the cutting-edge development version you just built and can modify.

  2. Enter conda create --name scipydev.
    This tells conda to create a virtual environment named scipydev. Note that scipydev can be replaced by any name you’d like to refer to your virtual environment.

  3. You’re still in the base environment. Activate your new virtual environment by entering conda activate scipydev.
    If you’re working with an old version of conda, you might need to type source activate scipydev instead (see here.

  4. (Optional) Enter conda list again. Note that the new virtual environment has no packages installed. If you were to open a Python interpreter now, you wouldn’t be able to import numpy, scipy, etc.

  5. Enter conda install cython numpy pytest spyder pybind11.
    Note that we’re only installing SciPy’s build dependencies (and Spyder so we can use the IDE), but not SciPy itself.

  6. Enter conda develop /scipy, where scipy is to be replaced with the full path of the SciPy root directory.
    This will allow us to import the development version of SciPy in Python regardless of Python’s working directory. Note: this step replace the steps shown in Anaconda SciPy Dev: Part II (macOS) that modify the ``PYTHONPATH`` environment variable when the ``scipydev`` virtual environment is activated. You can ignore that part of the video from 0:38 to 1:38; this is much simpler!

  7. In a new terminal window, test your setup. If you activate your virtual environment (e.g., conda activate scipydev) and run Python code that imports from SciPy, any changes you make to the SciPy code should be reflected when the code runs. After deactivating the virtual environment (conda deactivate), Python imports from the version of SciPy installed by Anaconda. You can also check which version of SciPy you’re using by executing in Python:

    import scipy
    print(scipy.__version__)
    

    If you have successfully imported a development version of SciPy, the word dev will appear in the output, e.g.:

    1.4.0.dev0+be97f1a