Skip to main content

A typical guide to use virtual environment for your python projects.

Virtual Environment is used to create isolated python environment. In simple terms it creates a virtual installation of Python and Pip into some directory. It is separated from your original python installation.


Why we need virtual environments?

Suppose you are working on some project and that project required more than 2 libraries that are dependent on each other. Generally these libraries are open sourced projects and maintained by different developers community. Now, if any one library gets changed in newer version and it is not backwards compatible and if you accidentally update that library from your global python installation then it might break your project.That's why it is safe to use virtual environments.

To create virtual environment in your machine please follow these steps:
  1. Make sure you have installed python in your computer.In this guide I will assume you have installed python 3.(make sure to check add to path)
  2. Install virtual env via this command
  3. $ pip install virtualenv
  4. Then create your virtual environment with following command
  5. $ virtualenv myproject
  6. To activate your virtual environment go to myproject directory and type this.
    for windows terminal
  7. $ cd myproject & scripts\activate
    for linux terminal
    $ source myproject/bin/activate
This will create your virtual environment named 'myproject' and then you can install your required modules using pip without affecting your original python environment.

Comments

Popular posts from this blog

Python 2 vs Python 3 Pystone benchmarking test!

When Python 3 came out, It was slower than Python 2. As Python's official blog says Python 3 is 10% slower than Python 2. So it means Python 2 is faster than python 3 by some margin. In this Benchmarking, we will use Pystone benchmarks to compare Python 2 and Python 3. My system specification is listed below: CPU = Intel core i3 2100 @ 3.10 GHZ GPU = Nvidia Geforce 210 RAM = 2 GB OS = Windows 10 Pro The benchmarking results may vary upon your system specification. Here's the result. python 3 pystone benchmarks python 2 pystone benchmarks As you can see python 2 has 24% higher benchmarks than python 3 which is pretty impressive. Maybe this is one of the reasons that some programmers prefers to use Python 2 instead of Python 3. Hope this helps :)

Want to use python 2 and 3 both on your windows pc? Here we go!

Step 1: First of all, you have to download both python 2 and 3 setup files.I am using Windows machine and so I have downloaded both python 2.7.12 and python 3.5 which are the latest version of python 2 and 3 respectively. (I recommend you to download 64-bit installer for both versions) Step 2: First, install python 2 and then python 3.Before installing python 3, make sure to check the box of Add python 3.5 to path as shown below. How to open both python shell in command prompt : If you want to use python shell in command prompt, you have to type the following as you can see in image below. How to open both python file in specified python version: If you want to open a file automatically by double clicking the .py file then you should specify which python version you are using.Then you should type the first line of python program as #!python2 and #!python3 for python 2 and python 3 as shown below. py2.py py3.py If you run the py2.p...

How to use pip for Python 3 alongside Python 2?

pip  is a package management system used to install and manage software packages written in  Python .You can find many third party packages on Python Package Index(PyPI).  There are currently   94060  packages here.You can browse all of the packages at the following link  PyPI packages So to use pip for Python 3 alongside Python 2, Follow this steps: Step 1: First of all, you have to install both python versions.If you haven't installed it already you can see my other post how to install python 2 and python 3 on Windows PC.                                           Make sure to check   Add python 3.5 to path while installing Python 3. Step 2: After installation, you have to setup your path for pip  (for Python 2) in your windows PC. To do that first of all copy the following path  C:\Python27\Scripts ...