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

  1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
    Python Projects for Students

    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    Project Center in Chennai

    ReplyDelete
  2. Your post is really good thanks for sharing these kind of post but if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in bangalore, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html

    ReplyDelete
  3. A big thank you for sharing this post your content is really good apart from that if anyone looking for best Core and Advanced Java training institute in delhi so contact here +91-9311002620 visit https://www.htsindia.com/java-training-courses

    ReplyDelete
  4. Really helpful right down to the ground; pleased to read such a helpful piece. I learned a lot from it, and I'll definitely remember it. Continue to spread the word custom erp development

    ReplyDelete

Post a Comment

Popular posts from this blog

Time and Timezones in Python

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