Python setup and creating virtual environment

The reason to write down this post is that I feel the Introduction to Python and Introduction to Django series I started I am using Microsoft Visual Studio as code editor. I feel that using MS Visual Studio specially for beginners like me is not a correct way to start. Specially for Python. Because MS Visual Studio no doubt a great code editor but it will do lot of things itself (like default settings, creating virtual environment), which good if you are expert but if you are trying to learn Python and Django like me then you have to first understand the basics and you should learn how to use PIP and create virtual environment.

Lets get started.  I am using Windows 10 for the development purpose. But I will update this article later for Linux as well. Install the latest version of Python for windows which is 3.5.1 when I was writing this post. Download the python version from Python website, and install it. This version will give you an option to install Python Path please select that so it will automatically add Python path variables otherwise you have to manually add them. Once installation complete open the command prompt and type python, it will display the python version.

Python versionTo exist from python environment use ctrl + Z .  If you not setup the the environment variables locate the python folder on your machine, in my case its C:\Python\Python35-32 open the environment variable window and setup the following path in user variable,

C:\Python\Python35-32\Scripts\;C:\Python\Python35-32\

Pip and Virtual Environment

Pip is a python package management tool. Its a command line program. Python is having a very active community and user are contributing there development with other users under open source license terms.  Pip is by default coming with Python 3.4 and so. Upgrade the Pip execute the following command in the command window, python -m pip install -U pip setuptools

Pyvenv is built-in tool available in Python for creating virtual environment. This tool is required to keep the dependencies for different project separate. There is tool virtualenv is available for Python. You can download the tool using Pip with following command pip install virtualenv . But If you are using Python version 3.3+ then there is a built-in tool available in Python. You need to run the following command to create a virtual environment on Windows,

c:\>c:\python35\python c:\python35\tools\scripts\pyvenv.py C:\Users\waqas\Documents\Projects\MyEnv

The explanation of above command line is at command prompt give python directory path which is my case is c:\python35\python  then type the path to scripts folder and file name pyvenv.py. And then give the path where you want to create virtual environment.

Now our virtual environment is ready. The next step is to activate it. To activate run the following command on command line

C:\>C:\Users\waqas\Documents\Projects\MyEnv\Scripts\activate.bat

Now our virtual environment is active and ready to use. To test the environment, open the Sublime Text editor and create a new file. And type a print statement print(“Hello from virtual environment”) , save the file in the root folder of the environment with the name HelloWorld.py.  Now execute the statement in the command line

(MyEnv) C:\Users\waqas\Documents\Projects\MyEnv>HelloWord.py

You will observe that after activating the environment the name of the environment is coming first on the command line.  Similarly to deactivate the environment run the following command (MyEnv) C:\Users\waqas\Documents\Projects\MyEnv\Scripts\deactivate.bat .

Leave a Reply