• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Change conda environments in the Vim editor (with Jedi-vim support)

Important Notice

This project is no longer supported.

You can keep in sending PR:s, but we encourage you to use vim-conda-activate instead.

Thanks to all the collaborators who took part into this journey. :)

vim-conda

This is a Vim plugin to support Python development using the Conda environment manager.

NOTE for Neovim users: If Neovim finds python on your $PATH, it assumes this is Python 2 (and likewise for python3 being treated as Python 3). If you start Neovim from a shell with an activated Conda env that uses Python 3, you're going to have problems because the conda env exposes a binary called python, but which is really 3 and not 2. Because of this, you will have to use the Neovim option of setting g:python_host_prog to point to a valid Python 2, into which you must also have pip installed the required neovim client.

Install

Vundle is the recommended way. Add this to the section in your vimrc file where all your plugin statements appear:

Plugin 'cjrh/vim-conda'

Edit: Vundle is no longer the recommended way!

I much prefer vim-plug which works in a similar way to Vundle, but seem just generally better all round. To add vim-conda, you need this:

Plug 'cjrh/vim-conda'

Super-short summary

When developing Python with Vim, there are two Pythons of interest:

  1. The one that executes your code in a shell command, i.e. :!python %
  2. The (embedded in Vim) one that jedi-vim uses to provide code completion.

Conda is concerned with the first one, i.e. the "shell Python". The second one depends on how you have Vim set up with respect to its own Python scripting support.

This plugin provides a command, CondaChangeEnv, that will

  1. Change the $PATH and $CONDA_DEFAULT_ENV environment variables inside the Vim process, so that new launched processes will have the same environment as if they were launched from a Conda env.
  2. Change the embedded Python sys.path inside Vim so that tools like jedi-vim will provide code completion for the selected env.

Demo

gif screencast of plugin demo

Introduction

The Vim editor can be used to develop Python code. One popular workflow is to edit the text of a code module (e.g. a .py file), and then execute the code with a shell command, such as

:!python %

(where % will be expanded to the name of the current file). Which python will run? Why, the one in the system path of course! But what happens if there is more than one Python executable in the system path? The first one to be found will be the one that runs.

This forms the basis of how virtual environments work. The Conda tool is an environment manager for Python; it also supports package management as part of its feature set, but we are not concerned with that here. Conda allows the user to create multiple, separate Python installations, and switch between them on the command line. It does this by modifying the $PATH (or PATH on Windows) environment variable.

vim-conda makes it easy to perform switching environments right from inside Vim. Now you never have to leave, so the >300 upvoted question on StackOverflow on "how to quit" need no longer concern you ;)

This plugin provides only one single command, CondaChangeEnv, which you can map to an unused key. You can call the command like so:

:CondaChangeEnv<ENTER>

You can map it to a key (e.g. in your vimrc file) like so:

map <F4> :CondaChangeEnv<CR>

When the command is executed, a wildmenu will appear showing the currently available Conda environments on your system. By selection one, the changes to $PATH and the embedded-python sys.path are made. (Unfortunately, the key for triggering wildmode and moving through the wildmenu is hard-coded to <Tab>; I still haven't learned enough vimscript to figure out how to use a user setting.)

In the list of environments, you will also see root as an option if you had previously changed to a non-root env. Selecting root is the same as doing a deactivate in the sense that all the changes made previously are rolled back.

Likewise, when you change from one environment to another, the change is clean in the sense that changes from the first env are reset, before changes for the new env are made. Exactly as would happen on the command line.

Details

The CondaChangeEnv command will trigger wildmode allowing you to tab through the existing Conda environments on your system. When an environment is selected, the following happens:

  • A new environment variable called $CONDA_DEFAULT_ENV is created inside the running Vim process
  • The $PATH variable is set to be the selected Conda env, plus the associated /Scripts folder, as per the usual way the activate script supplied by Conda would modify the path. Note that the $PATH environment variable inside the running Vim process is modified.
  • The sys.path list of the embedded Python instance inside the running Vim process is modified to include the entries for the selected Conda env. This is done so that the Jedi-Vim package will automatically be able to perform code completion within the selected env.

Notes

While testing, I found that I needed the following settings in my vimrc in order to suppress some errors related to other packages:

let g:jedi#force_py_version = 2
let g:UltisnipsUsePythonVersion = 2

More testing is needed to make sure that all the configurations work.

In order to suppress the message of vim-conda environment information on vim startup - add the variable in the 'vimrc' file.

let g:conda_startup_msg_suppress = 1

In order to keep the message of vim-conda environment information on vim startup - you can either comment out the above variable or add the below variable in the 'vimrc' file.

let g:conda_startup_msg_suppress = 0

In order to avoid a warning when opening vim without an environment activated, add the variable in the 'vimrc' file.

let g:conda_startup_wrn_suppress = 1

More Repositories

1

aiorun

A "run" function for asyncio-based apps that does all the boilerplate.
Python
417
star
2

easycython

An easy way to convert a .pyx file into a .pyd file and avoid having to write a setup.py
Python
85
star
3

autoslot

Automatic __slots__ for your Python classes
Python
60
star
4

thesauromatic

Static, offline, command-line CLI thesaurus
Rust
18
star
5

aiomsg

Smart sockets and scalability protocols for messaging network applications
Python
15
star
6

misu

High-speed physical quantities and dimensions in Python
Python
14
star
7

lifter

Download and sync new releases of single-file binaries from Github Releases and other sites
Rust
13
star
8

deadpool

A Python Process Pool Executor implementation that is harder to break
Python
13
star
9

coroexecutor

A CoroutineExecutor for asyncio, similar to nurseries and task groups
Python
13
star
10

dockerctx

Context manager for managing the lifetime of a docker container
Python
8
star
11

excitertools

itertools (and more-itertools) in the form of function call chaining (fluent interface)
Python
8
star
12

biodome

Better environment variables for Python
Python
8
star
13

pyconau2018-arcade2Dmultiplayer

Working files for my talk at PyCon AU 2018
JavaScript
6
star
14

osx-finder-toolbar-apps

Tools to be added to the OSX Finder toolbar
4
star
15

clonymccloneface

Clone all your github repos AND set upstream for your forks
Rust
4
star
16

sqlite-server

A network server for sqlite databases
Python
3
star
17

google-images-downloader

An experiment in browser automation
Python
3
star
18

debussy

CLI emitting json when song changes on Linux. Uses MPRIS and dbus to receive events
Rust
3
star
19

biodome-rs

Neater management of environment variables (intended for microservices)
Rust
2
star
20

dialetto

Language detection. Finally one that works with short text.
Rust
2
star
21

chat

Python
2
star
22

mucro

Easy executable symlinks to your virtualenv Python scripts
Python
2
star
23

aiodec

Decorators for coroutines
Python
2
star
24

aiologfields

Automatically inject Task-context-aware fields into loggers
Python
1
star
25

brispy-cython-workshop

Notebook files from the workshop
Jupyter Notebook
1
star
26

empatico

Emotion detection microservice based on natural language inference
Python
1
star
27

linux-toolbox-gui

GUI App for common tasks
Rust
1
star
28

chained-iterable

Python iterables in a functional-programming style
Python
1
star
29

cjrh_template

A very thin string.Template subclass with extra features.
Python
1
star
30

dictomatic

Static, offline, command-line CLI dictionary (including word definitions)
Rust
1
star
31

bumpymcbumpface

Version bump, tag and deploy your python packages
Python
1
star
32

sqllogformatter

A Python logging Formatter subclass for messages with SQL queries
Python
1
star
33

copydir

Recursively copy a directory tree, with filters.
Python
1
star
34

scalable-cuckoo-filter-py

Python wrapper for sile/scalable_cuckoo_filter
Rust
1
star
35

yet-another-python-indent

Back to basics indentexpr for Python
Vim Script
1
star