Skip to content

Instantly share code, notes, and snippets.

@Sakib37
Last active June 22, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sakib37/1885b046dde18316a15d94c7ab595573 to your computer and use it in GitHub Desktop.
Save Sakib37/1885b046dde18316a15d94c7ab595573 to your computer and use it in GitHub Desktop.
Pythong virtual envrionment setup
# script for setting up pip and virtualenv on Ubuntu 16.04
# Details can be found below
#!/usr/bin/env bash
sudo apt-get install virtualenv python3-pip -y
pip3 install --upgrade pip
# Installs virtualenvwrapper in ~/.local/bin/
sudo pip3 install --user virtualenvwrapper
# (preferred)
# Installs virtualenvwrapper in /usr/bin/
sudo pip3 install virtualenvwrapper
if `grep -q "source ~/.local/bin/virtualenvwrapper_lazy.sh" ~/.bashrc`;
then
echo "Virtual environment already exporte in ~/.bashrc file"
else
cat << EOF >> ~/.bashrc
# Virtual env
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # Set python version
export VIRTUALENVWRAPPER_SCRIPT=~/.local/bin/virtualenvwrapper.sh
source ~/.local/bin/virtualenvwrapper_lazy.sh
EOF
# If while creating virtualenv the following meesage appears which means python cannot load virtualenvwrapper module
# "/usr/bin/python: No module named virtualenvwrapper"
# then verify that $VIRTUALENVWRAPPER_PYTHON point to the right version of python for which virtualenvwrapper is installed
# For python3 it should be
# $ ls -l $VIRTUALENVWRAPPER_PYTHON
# lrwxrwxrwx 1 root root 9 Mär 20 2018 /usr/bin/python3 -> python3.5
fi
# the source command doesn't start a new shell instance but uses the current shell so the changes remain.
source ~/.bashrc
exec bash
#################### END of Script #####################################
# Efficiently manage virtual environment
# source : https://virtualenvwrapper.readthedocs.io/en/latest/
http://askubuntu.com/questions/244641/how-to-set-up-and-use-a-virtual-python-environment-in-ubuntu
# http://docs.python-guide.org/en/latest/dev/virtualenvs/
#
#
# Install virtualenv
sudo apt-get install virtualenv (Ubuntu 16.04 or above)
- Check version
virtualenv --version
# First install pip and then use pip to install virtualenvwrapper
# so that virtualenvwrapper will be autometically install for that python vertion as pip
# Install pip for python3
sudo apt-get -y install python3-pip
pip3 install --upgrade pip
- Check pip version
pip3 --version
# Install virtualevnwrapper
Because we want to avoid "sudo pip" we install virtualenvwrapper locally
(by default under ~/.local) with:
sudo pip3 install --user virtualenvwrapper
# Do the following steps to set virtualenvwrapper for lazy landing
Source- http://virtualenvwrapper.readthedocs.io/en/latest/install.html#lazy-loading
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # Set python version
export VIRTUALENVWRAPPER_SCRIPT=~/.local/bin/virtualenvwrapper.sh
source ~/.local/bin/virtualenvwrapper_lazy.sh
# Let's Create a virtualenv
mkvirtualenv test (If this command hangs check "ls -l $VIRTUALENVWRAPPER_PYTHON" or change shell)
mkvirtualenv test -p python3 # For python3 virtual environment
# Work on the new virtual environment
workon test
# Deactivate running virtual environment
deactive
# Delete the environment
rmvirtualenv test
# Save and use the packages in a virtual environment
pip3 freeze > requirements.txt
# To load all the packages from requirements.txt
pip install -r requirements.txt
#############################################################################################
############################# Errors ###################################
#############################################################################################
# If sourcing .bashrc gives "bash: /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh: No such file or directory"
- Uninstall virtualenvwrapper and remove the following file
sudo rm /etc/bash_completion.d/virtualenvwrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment