Skip to main content

Bash helpers for navigating and managing Python VirtualEnvs.

Project description

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 and auto-activation of VirtualEnvs relevant to your project (or executable). Just say:

~/work/project-x$ envie manage.py migrate

~/work/project-y$ envie python tests.py

~$ envie python playground/plucky/tests/tests.py

or use it in a hash bang:

#!/usr/bin/env envie
import os
print(os.getenv("VIRTUAL_ENV"))

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).

Summary

  • mkenv [-p <pyexec>] [-2|-3] [<env>] -- [virtualenv opts] - Create virtualenv in <env> based on Python version <pyexec>.

  • rmenv - Destroy the active environment.

  • chenv [-1] [-q] [-v] - Interactively activate the closest environment (looking down, then up, with lsupenv).

  • lsenv [-f|-l] [<dir>|"." [<avoid>]] - List all environments below <dir> directory, skipping <avoid> subdir.

  • lsupenv [-f|-l] [<dir>|"."] - Find the closest environments by first looking down and then dir-by-dir up the tree, starting with <dir>.

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

  • envie - Activate the closest virtual environment (if unambiguous).

  • 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 index - (Re-)index virtual environments (for faster searches with locate).

  • envie config - Interactively configure envie.

Install

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

The second line above will run a short configuration/setup procedure. 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.

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$

Change/activate environment

Use 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$ chenv
(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 chenv (cde<TAB> should actually do it, if you use tab completion):

stevie@caracal:~/demo/project1/src/deep/path/to/module$ chenv
(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$ chenv
1) ./project1/env
2) ./project2/env
#? 2
(env)stevie@caracal:~/demo$ which python
/home/stevie/demo/project2/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 lsupenv. chenv uses lsupenv when searching for environment to activate.

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.10.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

envie-0.4.10-py2.py3-none-any.whl (14.2 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for envie-0.4.10.tar.gz
Algorithm Hash digest
SHA256 bb856018295b5e259a662fb1dcf47886c799b4ea567489d1aaad7e3483f632cf
MD5 c6c919d0d3d639e5ec486d43b1753954
BLAKE2b-256 b15187aad88a7535bf8410c432cb7b1d3918eb413dbe129b0b13acd1258f9069

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for envie-0.4.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f27631bd8d615adb7ba41cb3d10730e6e7c39ccae958a93d361cbc724809e5f0
MD5 a6f21b9cd3a1a7c96f0bafdea6f7ffe2
BLAKE2b-256 3ea6b3a85adf677073354336e06f7544713e5a0a17961a837d3dec52b1eac271

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