For operation systems: Ubuntu, Debian, and MacOS. In windows the pip and virtualenv commands should be the same once python is installed somewhere.
Use python module psutil
as example.
> sudo apt update
> sudo apt install python-pip virtualenv
> /usr/bin/virtualenv ~/venv # create virtualenv in ~/venv
> source ~/venv/bin/activate # enter venv
(venv) > pip install psutil # install python module in venv
Need to enter venv
(source ~/venv/bin/activate
) in order to run python scripts which
use the module psutil
. If don't want to manually activate there are two ways:
- Add the following line to
~/.bashrc
(bash) or~/.zshrc
(zsh)source ~/venv/bin/activate
- Install
psutil
globally (for the user) without using virtualenv:> /user/bin/pip install psutil --user
Personally I prefer NOT to install globally for the user (exceptions: pylint/pyflakes). Sometimes, for some reason, there are multiple python in $PATH and it's confusing which python/pip and $PYTHONPATH is using (in my mac for example, I mainly use python2/3 from miniconda, but there are also python from macports due to dependencies and python from MacOS).
In mac, install pip via get-pip.py or anaconda/miniconda or macports or homebrew or the python installer from python.org (pip included). Apart from get-pip.py, all other methods also install a python binary somewhere.
Then install virtualenv via pip (or use conda/port/brew if install python and pip via these package managers).
For example (in mac, use get-pip.py, install pip, virtualenv, create virtualenv venv
, and
in venv
install python module psutil
):
> curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
> /usr/bin/python get-pip.py --user # make sure using the python2.7 in MacOS
# now pip is in ~/Library/Python/2.7/bin
> ~/Library/Python/2.7/bin/pip install virtualenv --user
> ~/Library/Python/2.7/bin/virtualenv ~/venv
> source ~/venv/bin/activate
(venv) > pip install psutil # install psutil in venv; note that pip is also from the virtualenv
One can add ~/Library/Python/2.7/bin
into $PATH
. I prefer not to do so. Once enter venv
there are already python and pip in $PATH
, so unless you need to install python modules
globally (for the user) or using the virtualenv command frequently, there's probably no need
to update $PATH
.
Also see above Debian/Ubuntu section in how to enter venv
automatically or install the
module globally for the user so that there's no need to source ~/venv/bin/activate
every time.
- enter venv:
~/venv/bin/activate
- leave venv:
deactivate
remove the venv: rm -rf ~/venv
To create a python3 virtualenv in Ubuntu/Debian:
> sudo apt install python3-pip
> /usr/bin/python3 -mvenv ~/py36env
> source ~/py36venv/bin/activate
In mac if there's already a python3 somewhere (here use python3 installed by macports as example):
> /opt/local/bin/python3.7 -mvenv ~/venv37
> source ~/venv37/bin/activate
anaconda/miniconda are probably more for scientific computing in python. minoconda is the lighter
version of anaconda. They come with a package manager conda
. They have their own commands to
manage environments, please refer to their documents. After create virtual environments, one can
use their conda
and/or pip
to install python modules.