Giter Club home page Giter Club logo

python-nix's Introduction

Python Development Environment

This project explains how to set up Nix + Python + Vim for python development with:

  • Auto-complete
  • Code audit
  • Offline documentation

screenshot

Requirements

  • git
  • vim (with the corresponding .vimrc)
  • nix
  • direnv (latest version with use_nix in the stdlib)
  • LSP for Python (python-language-server)
  • Vim plugins:
    • direnv-vim
    • jedi-vim: offline docs / go to definition
    • YouCompleteMe: autocomplete
    • syntastic: linting

Installation

  1. Add direnv-vim, jedi-vim, YouCompleteMe, and syntastic to your vim plugins (I use home-manager but you can do it manually).
  2. Add the following line to .vimrc to prevent jedi-vim to autocomplete (jedi-vim doesn't work well on big dependencies such as pandas):
let g:jedi#completions_enabled = 0
  1. Install direnv in NixOS (on zsh you need to add the following line eval "$(direnv hook zsh)" to .zshrc).
  2. Clone the template https://github.com/monadplus/python-template-nix and extract all files (you can delete the README).
  3. $ allow direnv . which should trigger your .envrc configuration.
  4. Open vim and enjoy*

*Auto-complete takes longer the first time, so you will need to wait ~30 seconds before it works (it is asynchronous so you can write in the meanwhile).

How it works

When you enter in the directory, direnv will automatically trigger the script inside .envrc.

This script, among other things, runs your shell.nix which setups a python interpreter with all dependencies required to run low-level python dependencies written in c, creates a virtual environment using venv and install all dependencies in the virtual environment using pip (feel free to add more dependencies to requirements.txt). pip installs flake8 (code audit) and LSP for Python which are essential for this setup.

When direnv has finished loading the initial setup (the first time always takes longer), you can start coding. Open any *.py file and auto-complete should work out of the box for all your dependencies. The same for offline documentation. Code auditing is triggered by syntastic after the file is saved.

Enjoy your coding :-)

Vim keybindings

Here are some useful commands for vim:

  • Goto assignment <leader>g (typical goto function)
  • Goto definition <leader>d (follow identifier as far as possible, includes imports and statements)
  • Goto (typing) stub <leader>s
  • Show Documentation K (shows a popup with assignments)
  • Renaming <leader>r
  • Usages <leader>n (shows all the usages of a name)
  • Open module, e.g. :Pyimport os (opens the os module)

Future work

For now only pip is supported as a package manager.

The implementation is ad-hoc and the setup is very manually. In the future, the shell.nix could install all dependencies (direnv, vim, etc)

Credit

The configuration started from a post on reddit: https://www.reddit.com/r/NixOS/comments/f4fvqp/nix_and_python/

python-nix's People

Contributors

monadplus avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.