Skip to content

Instantly share code, notes, and snippets.

@misho-kr
Last active November 19, 2018 06:26
Show Gist options
  • Save misho-kr/9faff33bfb755a7eee9da3c465d8c03a to your computer and use it in GitHub Desktop.
Save misho-kr/9faff33bfb755a7eee9da3c465d8c03a to your computer and use it in GitHub Desktop.
Summary of "Conda for Building & Distributing Packages" course at DataCamp.Com

Summary

In the Conda Essentials course you learned how use the Conda package manager to create and share reproducible environments for data science development.

Anaconda Projects

In this chapter you'll create an Anaconda Project, which is a data science asset that specifies package installs, file downloads, and executable commands. Anaconda projects can be used to run Jupyter notebooks, Bokeh server apps, REST APIs, and command line tools on Windows, Mac OSX, and Linux platforms making deployment easy.

  • Setup
> conda install anaconda-project
# create conda environment
> anaconda-project prepare
> anaconda-project list-commands
> anaconda-project run <command>
  • Create anaconda project
# create anaconda-project.yml
> anaconda-project init
  • Commands -- Anaconda Projects can support four types of commands:
    • Unix commands: shell-based commands that run on Mac or Unix systems
    • Windows commands: Windows shell commands
    • Bokeh App: Run bokeh-server with a given Python script
    • Jupyter Notebook: Launch Jupyter Notebook with the specified notebook file
> anaconda-project add-packages python=3 pandas statsmodels
> anaconda-project add-download ENV URL
> anaconda-project add-command NAME CMD
# update and lock the project
> anaconda-project update
> anaconda-project lock
  • Share the project
> anaconda-project archive <zip-file>
> anaconda login --username <name>
> anaconda upload <zip-file> --package-type=project

Python modules and packages

In this chapter you'll learn how to turn your Python scripts into importable modules, collect those modules into packages and create Conda packages that can be easily installed by other users.

A Python package is a collection of separate modules collected under a single name that share metadata, such as documentation, licensing, and version numbering. A Python package can have any number of directories and module source files. To define what gets imported from a directory you need a file called __init__.py.

  • Setup function from setuptools package
  • LISENCE file
from setuptools import setup, find_packages
from version import find_version

setup(
        name = 'mortgage_forecasts',
        author = 'Me',
        description = '30-year mortgage rate models',
        license = 'MIT',
        version = find_version('mortgage_forecasts', '__init__.py'),
        packages = find_packages()
     )

Conda packages

  • Install Conda Build
> conda install conda-build
  • Prepare meta.yml
{% set setup_py = load_setup_py_data() %}

package:
    name: 'mortgage_forecasts'
    version: {{ setup_py.get('version') }}

source:
    path: ./

build:
    script: <command to install>
    # script: python setup.py install <args>

requirements:
    run:
        # Packages that must be installed
        # in the user's conda environment
        # to run this package.

    build:
        # Packages used by setup.py
        # to install this package.
        # May also install compilers
        # for non-python code.

about:
    license: {{ setup_py.get('license') }}
    license_file: LICENSE
    summary: {{ setup_py.get('description') }}
  • Build and install the package
> conda build <package name>
> conda search --use-local --info <package name>
> conda install --use-local <paackage name>

The general practice is to run conda build separately on Windows, Mac, and Linux and with the popular minor revisions of Python (2.7, 3.5, 3.6, etc.). This step is necessary if you have architecture-specific build steps, like compiling Python C/C++ extensions, or incompatibility between Python 2 and Python 3 in both the build and run steps.

If possible, convert it to a noarch Conda package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment