Skip to content

Instantly share code, notes, and snippets.

@NishantJoshi00
Last active February 15, 2020 12:16
Show Gist options
  • Save NishantJoshi00/d5d61c90dbd4fb9d68ec971f79f2aae9 to your computer and use it in GitHub Desktop.
Save NishantJoshi00/d5d61c90dbd4fb9d68ec971f79f2aae9 to your computer and use it in GitHub Desktop.
A little cheatsheet for virtualenv in python

Cheatsheet of virtualenv for python3

What is VirtualEnv

PyPi Page: Click Here!

This package allows you to make seperate environment for your projects to manage the dependencies. Keeping all the packages in sync with one another. It helps in keeping the modules required for your project seperate from the main python environment. Making the project easily manageable, and making collaboration easy.

Installation

To install virtualenv in Windows, Mac or Linux

pip3 install virtualenv

Usage

Creating a new environment

To create a new environment for your project

virtualenv <ENV>

<ENV> : Name of your environment

This command creates a new directory in the current working directory named <ENV>

Activating This new Environment

To activate the environmet that you have newly create use the command

  • For Linux & Mac
    $ source /path/to/<ENV>/bin/activate
  • For Windows
    > \path\to\env\Scripts\activate

Why to use?

This new environment is a barebone version of python that we use but with the default dependiences new dependencies can be add using the pip command as you would do for normal python usage. But these modules would be installed in the virtualenvironment ratherthan the default python.

Deactivating

For deactivating this environment the command for Linux, Unix or Windows is same

deactivate

Advantages

  • Using virtualenv helps in managing modules conflit for different softwares
  • We can easily create a requirements file for a specified project with minimum dependencies
    pip freeze > requirements.txt
    # This can then be installed in a new virtualenv like
    virtualenv <ENV>
    # After activating the <ENV>
    pip install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment