Skip to content

Instantly share code, notes, and snippets.

@luisprox
Last active November 10, 2023 12:17
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 luisprox/6b75dfb972a01793ed200accbaa056b1 to your computer and use it in GitHub Desktop.
Save luisprox/6b75dfb972a01793ed200accbaa056b1 to your computer and use it in GitHub Desktop.
[Python virtualenv quick start] Quick step by step guide to use python virtual environment #python #virtualenv

Python Virutal Environment Quick Guide

Default venv that comes with new versions of python3 is a subset of the more complete virtualenv. So it is recommended to use the more complete virtualenv.

In Linux

  1. Install virtual environment
$ sudo apt install python3-virtualenv
  1. Create a new folder to be used by the new environment
# It is recommended to create an overal folder ~/.virtualenv and then create environment inside this folder.
$ mkdir ~/.virtualenv
  1. Enter to the environment folder
$ cd ~/.virtualenv
  1. Create a new environment in the current folder
$ virtualenv myEnv
$ virtualenv -p /usr/bin/python2.7 py27_env
  1. Created structure
# python3 myEnv structure
$ tree -L 3
.
└── myEnv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate.nu
    │   ├── activate.ps1
    │   ├── activate_this.py
    │   ├── deactivate.nu
    │   ├── pip
    │   ├── pip3
    │   ├── pip-3.10
    │   ├── pip3.10
    │   ├── python -> /usr/bin/python3
    │   ├── python3 -> python
    │   ├── python3.10 -> python
    │   ├── wheel
    │   ├── wheel3
    │   ├── wheel-3.10
    │   └── wheel3.10
    ├── lib
    │   └── python3.10
    └── pyvenv.cfg
  1. Activate virtualenv
$ cd ~/.virtualenv
$ source myEnv/bin/activate
(myEenv) $
  1. Review and install packages
# check current python version
(myEnv) $ which python
(myEnv) $ python --version
# verify installed packages
(myEnv) $ pip list
# install new packages
(myEnv) $ pip install numpy scipy matplotlib
# save current installed packages to requirements.txt
(myEnv) $ pip freeze --local > requirements.txt
# later on another environemnt, install requirements
(newEnv) $ pip install -r requirements.txt
  1. Deactivate current venv
(myEnv) $ deactivate
  1. Delete environment
# simply delete the folder created by the environemnt
$ rm -rf myEnv

Note: Do not create or put your project folder and files inside the virtualenv folder. It should be used only to manage the working version and packages.

In Windows

  1. Install virtual environment
> pip install virtualenv
  1. Create a new folder to be used by the virtual environment
> cd C:\Users\<username>
> mkdir virtualenv
  1. Enter to the environment folder
> cd virtualenv
  1. Create a new environment in the current folder
> python -m venv myEnv
  1. Activate virtualenv
> myEnv\Scripts\activate
(myEenv) >
  1. Review and install packages
rem check current python version
(myEnv) > python --version
rem verify installed packages
(myEnv) > pip list
rem install new packages
(myEnv) > pip install numpy scipy matplotlib
rem save current installed packages to requirements.txt
(myEnv) > pip freeze --local > requirements.txt
rem later on another environemnt, install requirements
(newEnv) > pip install -r requirements.txt
  1. Deactivate current venv
(myEnv) > deactivate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment