Skip to content

Instantly share code, notes, and snippets.

@ijash
Last active October 1, 2019 08:28
Show Gist options
  • Save ijash/6169695a4ced80dbb818af90cf827487 to your computer and use it in GitHub Desktop.
Save ijash/6169695a4ced80dbb818af90cf827487 to your computer and use it in GitHub Desktop.
Python Virtual environment Guide for Debian based Linux

Python Virtual environment Guide

For Debian based GNU/Linux

Vitrualenv

What does virtualenv do?

A lot of built in programs in Linux, OSX or POSIX compilant often rely on the system python, the executable to which, is in /usr/bin/python. Because a lot depend on it, it is not a good idea to mess with it, change it's version, or add new libraries into it.
The better way is to have the dependencies contained within it's own environment, so system python changes (due to upgrade) and other environment's changes don't affect the current project.
the steps below mostly compatible with Debian based GNU/Linux. however, it may possible on OSX and other linux distributions too. The main idea is just the same.

Steps:

  1. Install virtualenv to bash or terminal(use pip3 for python3):
 pip install virtualenv

or

sudo pip install virtualenv

or

sudo apt install virtualenv
  1. Make a directory wherever suits you. This directory will be used for several virtual environments. for example in terminal:
mkdir python_enviroments
cd python_enviroments

then, call virtualenv command and give your environment name to it. for example:

virtualenv env1

it will create a folder named env1 with all of the python tools in it, but it's not yet usable.

  1. To start using it, you need to be on the environment folder (env1) to activate the virtual environment and execute the activate file:
cd env1
source bin/activate

this will activate your python virtual envinroment. you can distinguish it by seeing your environment folder in parenthesis before the prompt. it will look similar to:

(env1) user@computer ~/python_enviroments/ $

this will separate your python dependencies and installed packages from main system.

  1. Now, you can install your required packages with pip install command.
    (optional)after installing the packages, save the dependencies into a text file by typing:
pip freeze requirements.txt
  1. After finished using the environment, you can exit the virtual environment by typing
deactivate

Python 3 venv

venv module, part of the standard Python 3 library

  1. If not yet included in your python3, use this line in terminal.
sudo apt-get install -y python3-venv
  1. then, create folder for all the envs:
mkdir environments
cd environments
  1. Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:
python3 -m venv my_env
  1. after it's done, you can activate it using:
source my_env/bin/activate
  1. (optional)to edit and include it on vscode just type it the current terminal
code .
  • Go to the parent folder in which venv is there through command prompt.
  • Type code . and Enter. [Working on both windows and linux for me.]
  • That should also show the virtual environments present in that folder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment