Command-line shortcuts for Python virtual environments
Project description
VIEN is a command-line tool for managing Python Virtual Environments in shell.
It provides one-line shortcuts for:
- creating and deleting environments
- running commands inside environments
- switching between environments in bash shell
Switching between projects should be simple. Creating environments for the projects should be simple too.
Ideally it's a short command that I would type even half asleep.
Something like
$ vien create
$ vien shell
And not like
$ python3 -m venv ./where/to/put/this/.venv
$ source /i/lost/that/.venv/bin/activate
Ready-made solutions did not help.
- pipenv kind of solved the problem, but brought new challenges unrelated to virtual environments
- virtualenvwrapper is a package whose name is easier to copy-paste than to type. I almost started creating virtualenvwrapperwrapper shortcuts
So there is the vien
. A tool for a half asleep developer.
Install
Get a working Python ≥3.7, pip3 and venv.
@ Ubuntu
$ sudo apt install -y python3 python3-pip python3-venv
@ macOS
$ brew install python3
Check it works
$ python3 --version # python shows its version
$ python3 -m venv --help # venv shows help message
$ pip3 install --upgrade pip # pip upgrades itself
Then:
$ pip3 install vien
Make sure it installed:
$ vien # shows help
Upgrade it later:
$ pip3 install vien --upgrade
Use
Create a virtual environment
$ cd /path/to/myProject
$ vien create
By default vien
will try to use python3
as the interpreter for the virtual environment.
If you have more than one Python version, provide one more argument.
Point to the proper interpreter the way you execute it.
If you execute scripts like that
$ python3.8 /path/to/script.py
Create virtual environment like that:
$ vien create python3.8
Or provide full path to the interpreter:
$ vien create /usr/local/opt/python@3.8/bin/python3
Dive into interactive bash
$ cd /path/to/myProject
$ vien shell
(myProject)$ _
Now you are inside the virtual environment.
(myProject)$ pip3 install requests # installs packages into virtualenv
(myProject)$ python3 use_requests.py # runs in virtualenv
(myProject)$ which python3 # we are using separate copy of Python
(myProject)$ echo $PATH # everything is slightly different
Get out of the virtual environment:
(myProject)$ exit
$ _
Now you're back.
Run a script inside the virtual environment
It is vien run <any bash command>
$ cd /path/to/myProject
$ vien run python3 use_requests.py arg1 arg2 # runs in virtualenv
$ vien run pip3 install requests # installs packages into virtualenv
is an equivalent to
$ cd /path/to/myProject
$ source /path/to/the/venv/bin/activate
$ python3 use_requests.py arg1 arg2
$ /path/to/the/venv/bin/deactivate
$ source /path/to/the/venv/bin/activate
$ pip3 install requests
$ /path/to/the/venv/bin/deactivate
Where are the virtual environments
vien
offers a simple rule of where to place the virtualenv.
project dir | virtual environment dir |
---|---|
/abc/thisProject |
$HOME/.vien/thisProject_venv |
/abc/otherProject |
$HOME/.vien/otherProject_venv |
/moved/to/otherProject |
$HOME/.vien/otherProject_venv |
So only the local name of the project directory matters. And all the virtual environments
are in $HOME/.vien
.
You can set the directory where vien
places the virtual environments. By default, it's $HOME/.vien
. If you're not happy with this, you can define the environment variable VIENDIR
:
$ export VIENDIR="/x/y/z"
So for the project aaa
the virtual environment will be located in /x/y/z/aaa_venv
.
The _venv
suffix tells the utility that this directory can be safely removed.
Other commands
Delete virtual environment
$ cd /path/to/myProject
$ vien delete
Delete old and create new virtual environment
Useful, when you want to start from scratch.
$ cd /path/to/myProject
$ vien recreate
Optionally point to the interpreter:
$ cd /path/to/myProject
$ vien recreate /path/to/bin/python3
Shell prompt
By default the vien shell
adds a prefix to the $PS1
bash prompt.
user@host$ cd /abc/myProject
user@host$ vien shell
(myProject)user@host$ _
So you can see, which virtual environment you're using.
If you customized your PS1
, it may not work as expected.
personalized:prompt> cd /abc/myProject
personalized:prompt> vien shell
(myProject)user@host$ _
It can be fixed by providing PS1
variable to vien
like that:
personalized:prompt> cd /abc/myProject
personalized:prompt> PS1=$PS1 vien shell
(myProject)personalized:prompt> _
To avoid doing this each time, export
your PS1
to make it available for subprocesses.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.