Setting up a virtual environment in Python

In this post, we are going to set up a virtual environment in Python and as an example install Django 1.8.18 (LTS).

Although you can set up a virtual environment in any distribution of Python, in this post we are going to use WinPython, a portable version of python that you can run from a USB drive on any Windows machine. No installation or special permissions are required!

If you already have a Python distribution installed that you want to use, you can skip to step 2  and just install virtualenv.

1. Install WinPython

Download your choice of WinPython from sourceforge.net. Here I chose WinPython-64bit-3.6.2.0Qt5.exe. Run the exe file and you will have a folder as shown in the image below. During the installation, or rather unpacking of the files, you must choose a destination folder. Here I chose to unpack the files to a folder called WinPython-64bit-3.6.2.0Qt5 that is located in the root of my drive, in my case d:\WinPython-64bit-3.6.2.0Qt5\. Although the folder name is long, it has the advantage to keep track on what version of Python you are running, but you can choose a shorter name, like d:\Python.

Contents of the destination folder after running WinPython-64bit-3.6.2.0Qt5.exe

2. Install virtualenv

If you installed WinPython as described above, then start WinPython Command Prompt.exe and install virtualenv by using pip,

D:\WinPython-64bit-3.6.2.0Qt5\Scripts>pip install virtualenv

This is what it looked like on my machine.


3. Create a virtual environment and install Django

You can have several different virtual environments with different packages installed on your machine. Each virtual environment is located in a folder. To get somewhat organized, I collect all my virtual environments in a top-level folder called virtenv, located in the root of drive D. But you can choose whatever name you like.

For this example, create a directory called virtenv and cd into it. The virtual environment will be created in this folder. Working in WinPython Command Prompt.exe type

D:\WinPython-64bit-3.6.2.0Qt5\Scripts>mkdir d:\virtenv

D:\WinPython-64bit-3.6.2.0Qt5\Scripts>cd d:\virtenv

d:\virtenv>

Choose a name for your virtual environment, for the sake of simplicity (or lack of imagination),  call it project1. Create the environment by typing

d:\virtenv>virtualenv project1

The folder virtenv now contains a sub folder called project1. This is the folder for the virtual environment in which we will install and experiment with Django 1.8.18 (LTS).  To start using this virtual environment you have to activate it. Type

d:\virtenv>project1\Scripts\activate

(project1) d:\virtenv>

Note how the command line starts with (project1). This is to indicate that you are running python in a virtual environment.

As a reference, this is a screen capture from my computer


Installing packages in this virtual environment is trivial, for example,  to install Django 1.8.18 (LTS) type

(project1) d:\virtenv>pip install Django==1.8.18

To check what packages are installed in your environment, run pip freeze

(project1) d:\virtenv>pip freeze

And that's it! You have a virtual environment called project1 and in this environment, we have installed Django 1.8.18 LTS.  A reference screen from my computer is shown below.

If you would like to see an example of how to set up a Django project then read section 3.


4. Set up a Django project in a virtual environment

Setting up a Django project using a virtual environment is straightforward. The first decision to make is, where do you want to store the code that you develop?  For this example, we are going to create a Django project called trydjango18, and we are going to store this project inside the virtual environment project1, this might not be the best practice, but it serves as an example.

Working in WinPython Command Prompt.exe type

(project1) d:\virtenv>cd project1

(project1) d:\virtenv\project1>django-admin startproject trydjango18


It is worth nothing that the command on windows is django-admin startproject trydjango18 and not django-admin.py startproject trydjango18 as written in some web sources.

After running this command you will have a Django project folder called trydjango18 . If you followed my set up, you will have this folder hierarchy

virtenv/
    project1/
        Include/
        Lib/
        Scripts/
        tcl/
        trydjango18/
             trydjango18/
                 __init__.py
                 settings.py
                 urls.py
                 wsgi.py
             manage.py
        pip-selfcheck.json

Note that you have two folders named trydjango18, an outer folder and an inner folder. This can be confusing, and it is ok to change the name of the outer folder. For convenience, change the name of the outer trydjango18 folder to src. In this folder we will develop the Django project.

After the name change, your folder hierarchy should look like this

virtenv/
    project1/
        Include/
        Lib/
        Scripts/
        tcl/
        src/
            trydjango18/
                __init__.py
                settings.py
                urls.py
                wsgi.py
            manage.py
        pip-selfcheck.json
     

You can check that Django is working by the first cd into the src folder and then starting the development server

(project1) d:\virtenv\project1\src>python manage.py runserver

If everything worked you should have a confirmation that the development server started. Visit http://127.0.0.1:8000/ and make sure you have the greeting page.


5. Conclusions & Summary

In this post, we have installed a portable version of Python for windows, WinPython, with the virtual environment package. To create and work in a virtual environment follow these steps:
  1. Start WinPython Command Prompt.exe
  2. Make a directory for your virtual environment and cd into this directory
  3. Create the virtual environment by typing virtualenv name
  4. Activate the virtual environment by typing: name/Scrits/activate
  5. Develop your code....
  6. End the virtual environment by typing: deacativate
If you have any comments use the section below.







 

Comments

Post a Comment

Popular posts from this blog

WinPython - Portable Python that you can run from a usb drive on any Windows machine

Time and Timezones in Python