Skip to main content

Fast and accurate Python dependency management

Project description

https://travis-ci.org/alexbecker/dotlock.svg?branch=master

Fast and safe dependency management for Python applications.

Supports Linux and OS X.

Motivation

The de-facto standard way to keep track of Python dependencies is with a requirements.txt file, listing the required packages and specifying what versions of them can be used. There are two strategies for specifying versions in a requirements.txt file: adding only the top-level dependencies and constraints you know to be necessary, or adding every recursive dependency and pinning them to specific versions you know work. The first strategy makes installing dependencies non-repeatable. The second makes upgrading difficult, and is hard to manage with standard python tools.

Dotlock enables you to do both: keep track of top-level requirements and known constraints in package.json, and generate repeatable requirement sets in package.lock.json by running a single command: dotlock lock.

Dotlock is partly inspired by pipenv, which also provides dependency-locking functionality. However, dotlock has a different philosophy: instead of acting as a wrapper around pip, dotlock handles package resolution natively. This gives dotlock more flexibility, better performance, and a smaller surface area for bugs, but at the cost of the wide platform and package support the pip developers have put so much work into. This should make dotlock better for deploying to common environments such as linux servers with common packages, but there will always be edge cases of platforms or packages it does not support.

Dotlock’s main goals are:

  • Accuracy: dotlock lock locks to the level of specific distributions, not just versions. That means you know exactly what code will be installed by dotlock install, and package.lock.json will contain a single hash for each package. You can even vendor your dependencies with dotlock bundle.

  • Speed: dotlock lock uses caching and asyncio to re-lock after changes to package.lock in 1s or less. Similarly, dotlock install and dotlock bundle download dependencies in parallel.

  • Extras Support: Unlike pipenv which only supports “default” dependencies and “dev” dependencies, dotlock supports arbitrary extra dependency groups, e.g. dotlock install --extras tests.

Installation

Dotlock can be installed with pip, i.e. pip install dotlock. It can be installed in an application’s virtual environment, at the user level, or globally.

Development Setup

On your development machine, run dotlock init to create a virtualenv and a skeleton package.lock file. Add your sources and dependencies to package.lock:

{
    "sources": [
        // PyPI-like package index hosting the dependencies.
        // If multiple indexes are included, each is tried in order during dependency resolution.
        "https://pypi.org/pypi"
    ],
    "default": {
        // Requirements in the form "package-name": "specifier".
        // Version specifiers may be "*", or a version number preceded by any of <, <=, >, >=, or ==.
        // Multiple specifiers can be separated by commas, e.g. ">=2.1,<3.0".
        "setuptools": ">=39.0",
        "virtualenv": "*",
        // Git, Mercurial and Subversion dependencies are also supported.
        "requests": "git+git://github.com/requests/requests@v2.19.1",
        // If you need extras or markers, supply a dictionary instead of a string.
        "idna_ssl": {
            "specifier": "*",
            "marker": "python_version < 3.7",  // See PEP 496
            "extras": ["tests"],
        },
        // Local file paths can be used too, but this loses integrity guarantees.
        "mypackage": "~/projects/mypackage"
    },
    "extras": {
        // You can specify groups of additional dependencies that will be installed by
        // dotlock install --extras [names]
        "dev": {
            "ipython": "*"
        },
        "tests": {
            "pytest": "*"
        }
    }
}

Then you can lock and install your dependencies:

dotlock lock  # Creates package.lock.json.
dotlock install  # Installs exactly the distributions in package.lock.json.
# Either source venv/bin/activate to enter the virtualenv, or use dotlock run.
dotlock run [program] [args]  # Runs [program] in the virtualenv.

For more information, run dotlock -h or dotlock [command] -h.

Developing in Different Environments

If your development environment differs significantly from your target deployed environment, e.g. you use a different operating system or a different version of Python, you will have to do some extra work and lose some of the benefits of dotlock.

In order to resolve dependencies and select distributions correctly, dotlock needs to know certain features of the deployed environment. Run dotlock dump-env on the deployed environment to create an env.json file. This file should live alongside your package.json file, and will be used by dotlock lock.

Since package.lock.json contains only the distributions appropriate for your deployed environment, running dotlock install on an incompatible environment will error. Instead, you can run dotlock install --skip-lock, which will bypass package.lock.json, looking just at package.json.

Deployment

There are two ways to install your locked dependencies during deployment:

  • Install dotlock and run dotlock install in the application root directory.

  • Use dotlock bundle to create bundle.tar.gz and install.sh prior to deployment, include these files in the deployment, and run ./install.sh during deployment.

Using dotlock bundle is preferred because it does not require installing dotlock in the deployed environment and does not depend on external services during deploy.

Once the dependencies are installed, run your application with one of:

  • source venv/bin/activate; [program] [args]

  • Assuming dotlock is installed: dotlock run [program] [args]

Roadmap and Limitations

Planned features:

  • Interpolate environment variables in sources

  • Allow specifying indices for individual packages

Features under consideration:

  • Support virtualenvs other than ./venv

  • Support installing into system python (useful in certain contexts, e.g. containers)

  • Support versions of Python before 3.6

  • Support locking for other platforms. This is not possible to do with perfect reliability, since the dependencies discovered by running setup.py may differ depending on what platform the script is run on.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dotlock-0.8.1.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

dotlock-0.8.1-py3-none-any.whl (46.7 kB view details)

Uploaded Python 3

File details

Details for the file dotlock-0.8.1.tar.gz.

File metadata

  • Download URL: dotlock-0.8.1.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for dotlock-0.8.1.tar.gz
Algorithm Hash digest
SHA256 5f2f985b7b62f9484f3171071815ee4c3a61290a4f2512eb8a492e7930656e0f
MD5 c20c64dafd373afcfcc3a2327924d318
BLAKE2b-256 4165ce45399e56b3dd419fe19df7294b224a058b3d5c9d11a3687e6865c36251

See more details on using hashes here.

File details

Details for the file dotlock-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: dotlock-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 46.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2

File hashes

Hashes for dotlock-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4440be144b199ab1510ab001eb8710a9328294a9d4f7536cd9b4b01638714ae
MD5 469188782aece3d18cb2ff08ff5ac897
BLAKE2b-256 e0a75bd22949f038014cadc96258896b9fc1c60c279e1b0dd192d59afad352a1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page