Giter Site home page Giter Site logo

matthewfeickert / venv-activate Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 15 KB

Bash creation and tab completion of activation of Python virtual environments

License: BSD 3-Clause "New" or "Revised" License

Shell 100.00%
venv python virtual-environments bash tab-completion

venv-activate's Introduction

venv-activate

Bash tab completion of creation and activation of Python virtual environments (installed under $HOME/.venvs by default)

Installation

If _venv-activate.sh is installed at /opt/_venv-activate/_venv-activate.sh then if the following is added to a user's ~/.bashrc

# Enable tab completion of Python virtual environments
if [ -f /opt/_venv-activate/_venv-activate.sh ]; then
    _VENV_ACTIVATE_HOME="${HOME}/.venvs"
    _VENV_ACTIVATE_PYTHON=$(which python3)
    . /opt/_venv-activate/_venv-activate.sh
fi

then a user can tab complete for possible Python virtual environments to activate with venv-activate (assuming that the path of VENV_ACTIVATE_HOME actually exists).

Example

$ venv-create data-science

results in


(data-science) $ python -m pip install --upgrade pip setuptools wheel

# Created virtual environment data-science

# To activate it run:

venv-activate data-science

# To exit the virtual environment run:

deactivate


and then

$ venv-activate #tab

results in

$ venv-activate
data-science

and tab completing data-science and hitting enter then activates the virtual environment

(data-science) $

API

venv-create

Create a new Python virtual environment.

$ venv-create sandbox

(sandbox) $ python -m pip install --upgrade pip setuptools wheel

# Created virtual environment sandbox

# To activate it run:

venv-activate sandbox

# To exit the virtual environment run:

deactivate

venv-activate

Activate an existing Python virtual environment.

$ venv-activate sandbox
(sandbox) $

venv-remove

Remove an existing Python virtual environment by deleting the venv directory (which holds the Python runtime and installed packages).

$ venv-remove sandbox

# Deleted virtual environment sandbox

venv-info

Show version information for Python and pip, and how many packages are installed inside the Python virtual environment.

$ venv-info sandbox

# Python   : v3.7.5
# pip      : v19.3.1
# installed: 1 packages

Authors

Primary Author: Matthew Feickert

venv-activate's People

Contributors

matthewfeickert avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

phinate

venv-activate's Issues

Apply Shellcheck recommendations

Using Shellcheck on _venv-activate.sh reveals the following issues:

$ shellcheck _venv-activate.sh 

In _venv-activate.sh line 8:
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    ^-- SC2034: prev appears unused. Verify it or export it.


In _venv-activate.sh line 9:
    opts="$(echo $($(which ls) -1 ${HOME}/venvs/ | sed 's/^//'))"
                 ^-- SC2046: Quote this to prevent word splitting.
                 ^-- SC2005: Useless echo? Instead of 'echo $(cmd)', just use 'cmd'.
                                  ^-- SC2086: Double quote to prevent globbing and word splitting.


In _venv-activate.sh line 12:
        COMPREPLY=($(compgen -W "${opts}" $cur))
                                          ^-- SC2086: Double quote to prevent globbing and word splitting.


In _venv-activate.sh line 19:
        $(which ls) -1 ${HOME}/venvs/ | sed 's/^/* /'
                       ^-- SC2086: Double quote to prevent globbing and word splitting.


In _venv-activate.sh line 28:
        printf "\n$1 is not a valid virtual environment."
               ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

These should be addressed and fixed. ๐Ÿ‘

Consider API for Python and pip info, pip list, pip freeze, pip show

Given _VENV_ACTIVATE_HOME and the name of the virtual environment to query/use the full path to any Python and pip runtime for any virtual environment is know. Given this, without having to activate the virtual environment information on it can be queried. For venv ${name} the full paths are:

  • Python: "${_VENV_ACTIVATE_HOME}"/${name}/bin/python
  • pip: "${_VENV_ACTIVATE_HOME}"/${name}/bin/pip

So something like venv-info could give information on Python and pip version

$ venv-info sandbox
Python: v3.7.5
pip: v19.3.1
packages installed: 37

and then mirroring pip one could do

$ venv-pip list sandbox
<output of sandbox's pip list>
$ venv-pip freeze sandbox
<output of sandbox's pip freeze>
$ venv-pip show numpy sandbox
Name: numpy
Version: 1.17.4
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /home/feickert/.venvs/sandbox/lib/python3.7/site-packages
Requires: 
Required-by: tensorflow-gpu, tensorboard, scipy, opt-einsum, Keras-Preprocessing, Keras-Applications, jaxlib, jax, h5py

The API could be improved though.

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.