Skip to main content

Bash helpers for navigating and managing Python VirtualEnvs.

Project description

https://img.shields.io/pypi/v/envie.svg https://img.shields.io/badge/platform-GNU/Linux,%20BSD-red.svg https://img.shields.io/badge/shell-bash-blue.svg https://img.shields.io/pypi/pyversions/envie.svg https://api.travis-ci.org/randomir/envie.svg?branch=master

At its core, envie is a set of Bash functions aiming to increase your productivity when dealing with mundane VirtualEnv tasks, like: creating, destroying, listing, switching and activating environments.

But envie really shines when it comes to auto-discovery, auto-activation and auto-creation of VirtualEnvs relevant to your project (or executable). Just say:

~/work/projectA$ envie python tests.py

~/work/projectB$ envie manage.py migrate

~$ envie run python -c 'import os; print(os.getenv("VIRTUAL_ENV"))'

or use it in a hash bang:

#!/usr/bin/env envie

or, just import it at the beginning of your Python program:

#!/usr/bin/python
import envie.activate_closest

and in each of these cases the Python script will be executed in the closest virtual environment (for the definition of the closest environment see below, section Change/activate environment).

To just activate the closest virtual env, just type envie:

~/work/my-project-awesome-cattoy$ envie

or even:

$ envie awesome toy

(keywords filter all virtual envs in vicinity and activate the best match - if unique; if not unique you’re prompted to select the exact environment you wish to activate)

Summary

  • envie / chenv [-1] [-f|-l] [-q] [-v] [<basedir>] [<keywords>] - Interactively activate the closest environment (looking down, then up, with findenv), optionally filtered by a list of <keywords>. Start looking in <basedir> (defaults to .).

  • envie create / mkenv [-2|-3|-e <pyexec>] [-r <pip_req>] [-p <pip_pkg>] [-a] [<envdir> | -t] -- [virtualenv opts] - Create virtualenv in <envdir> (or in temporary dir, -t) based on Python version <pyexec>, optionally install Pip packages from <pip_req> requirements file and <pip_pkg> package specifier.

  • envie remove / rmenv - Destroy the active environment.

  • envie list / lsenv [-f|-l] [<dir>] [<keywords>] - List all environments below <dir> directory, optionally filtered by a list of <keywords>.

  • envie find / findenv [-f|-l] [<dir>] [<keywords>] - Find the closest environments by first looking down and then dir-by-dir up the tree, starting in <dir>; optionally filtered by a list of <keywords>.

  • envie python <script>, envie <script> - Run python script in the closest virtual environment.

  • envie run <command> - Execute arbitrary command/builtin/file/alias/function in the closest virtual environment.

  • envie config - Interactively configure envie.

  • envie index - (Re-)index virtual environments (for faster searches with locate).

  • envie help - Print usage help. For details on a specific command use the ‘-h’ switch (like envie find -h, or mkenv -h).

  • cdenv - cd to the base dir of the currently active virtualenv ($VIRTUAL_ENV).

Install & configure

For convenience, envie is packaged and distributed as a Python package. To install, simply type:

$ sudo pip install envie
$ envie config

# start clean:
$ . ~/.bashrc

# or, open a new shell

After install, be sure to run a (short and interactive) configuration procedure with envie config. If in doubt, go with the defaults.

By default, envie sourcing statement is added to your .bashrc file, locate index is set as a preferred source (it’s set to be rebuilt every 15m, or on demand), with all relevant environments’ ancestor dir set to your $HOME directory.

Testing

Run all test suites locally with:

$ make test

(after cloning the repo.)

Examples

Create/destroy

To create a new VirtualEnv in the current directory, just type mkenv <envname>. This results with new environment created and activated in ./<envname>. When done with this environment, just type rmenv to destroy the active env.

stevie@caracal:~/demo$ ls
stevie@caracal:~/demo$ mkenv env
Creating python environment in 'env'.
Using Python 2.7.9 (/usr/bin/python).
(env)stevie@caracal:~/demo$ ls
env
(env)stevie@caracal:~/demo$ pip freeze
argparse==1.2.1
wsgiref==0.1.2
(env)stevie@caracal:~/demo$ rmenv
stevie@caracal:~/demo$ ls
stevie@caracal:~/demo$

Create Python 3 environment in env and install pip packages from requirements.txt:

$ mkenv3 -r requirements.txt

Create a throw-away environment with a pre-installed dev-requirements.txt and a local project in editable mode from /home/stevie/work/mypackage/:

$ mkenv -t -r dev-requirements.txt -p "-e /home/stevie/work/mypackage/"

To automate the previous example, you can use envie-oneoff command in your hashbang, like this:

#!/usr/bin/env envie-oneoff
# -*- requirements: ./path/to/my/requirements.txt -*-

<your python code here>

When executed, a throw-away virtualenv is created, requirements specified are installed inside, code is run, and the environment is destroyed afterwards. Other way to do it is directly: envie-oneoff SCRIPT.

Change/activate environment

Use envie (base command), or the explicit chenv to activate the closest environment, tree-wise. We first look down the tree, then up the tree. If a single Python environment is found, it’s automatically activated. In case the multiple environments are found, a choice is presented to user.

stevie@caracal:~/demo$ ls -F
env/ project/ file1 file2 ...
stevie@caracal:~/demo$ envie
(env)stevie@caracal:~/demo$

Assume the following tree exists:

~/demo
  |_ project1
  |  |_ env
  |  |  |_ ...
  |  |_ src
  |     |_ ...
  |_ project2
  |  |_ env
  |     |_ ...

Now, consider you work in ~/demo/project1/src/deep/path/to/module, but keep the environment in the env parallel to src. Instead of manually switching to env and activating it with something like source ../../../../../env/bin/activate, just type envie (or chenv):

stevie@caracal:~/demo/project1/src/deep/path/to/module$ envie
(env)stevie@caracal:~/demo/project1/src/deep/path/to/module$ which python
/home/stevie/demo/project1/env/bin/python

On the other hand, if there are multiple environments to choose from, you’ll get a prompt:

stevie@caracal:~/demo$ envie
1) ./project1/env
2) ./project2/env
3) ./projectx/env
#? 2
(env)stevie@caracal:~/demo$ which python
/home/stevie/demo/project2/env/bin/python

If you know the name of your project (some specific path components – keywords), you can preemptively filter, and auto-activate the project environment with:

stevie@caracal:~/demo$ envie x
(env)stevie@caracal:~/demo$ which python
/home/stevie/demo/projectx/env/bin/python

Search/list environments

To search down the tree for valid Python VirtualEnvs, use lsenv. Likewise, to search up the tree, level by level, use findenv. chenv uses findenv when searching for environment to activate.

Suppose in your work directory you have projects named trusty and zesty. And for both of them you keep dev and prod env:

$ lsenv dev

./work/trusty/dev
./work/zesty/dev

or to activate trusty dev, all you need to type is:

$ envie t d

Activated virtual environment at './work/trusty/dev'.

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

envie-0.4.27.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

envie-0.4.27-py2.py3-none-any.whl (28.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file envie-0.4.27.tar.gz.

File metadata

  • Download URL: envie-0.4.27.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for envie-0.4.27.tar.gz
Algorithm Hash digest
SHA256 53a4568aff2cb37a93885815e9e65b86aa7c4e75f10178815e3b830b26fbb6ba
MD5 367c12c999db7566167660c39493003c
BLAKE2b-256 daead1f3f793784b2f1253bcdc23ddd42acb6bea4bb701991150c835297512ab

See more details on using hashes here.

File details

Details for the file envie-0.4.27-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for envie-0.4.27-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 59f942ba2e1f87d12ae8086a4241db66a0331bf7d7f61c7e3989f3a96daefe76
MD5 02325eeb70d7780143d256e7356087d5
BLAKE2b-256 05cae2a6ed04b452bf0950a5c4dc3b981cb15ca59068f5e2545da72e5d1d5ae7

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