Django (RESTful API) + ReactJS (with Redux) Tutorial: Installing Django with Virtual Env (Part 1)

in utopian-io •  7 years ago  (edited)


Source: Django Project

The above words couldn't be truer. I couldn't count the instances wherein Django has been a tremendous help. Django is a high-level web framework which was created for quick web project development. It lets you build apps on a much faster rate with less code. This is my go-to web framework every time I'm tasked with a project that already had a deadline way before it is delegated to me. For those who are interested in learning Python as a server-side language, these series of tutorials are just for you. This will be part one of a multi-series tutorial wherein we are going to build a single page application using Django (RESTful API) + React and Redux then finally deploy it to the cloud (Heroku).

What Will I Learn?

At the end of this tutorial, you are going to be able to:

  • Define the use of a virtual environment
  • Install and use virtualenv
  • Learn how to activate and deactivate the virtual environment

Requirements

Difficulty

This tutorial will assume that you already know Python and have it already installed. You should also know the basics like what is a web framework and what are packages or modules. For now, what we will discuss are basic topics. However, we will go into the more difficult lessons as we go along.

Contents

I. What is Django?

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. Visit the official documentation for more information.

II. What is a virtual environment and why do I need it?

Usually, your python projects often use packages or modules that are not part of the standard library. Projects will sometimes need a specific version of a package in order to work. You will absolutely need it to be accurate to avoid conflicts and bugs or to avoid using an obsolete version of the library's interface.

If we are going to use the Python dependencies installed in our computers, conflict between different projects will arise. For example, app A needs version 1.0 of Z library. We then also try to use app B which uses version 2.0 of Z library. Currently we have 2.0 of Z library. This means that app A will not run in our desktop environment. This situation is exactly why virtual environments were made.

A virtual environment is a self-contained directory tree that contains a specific version of Python plus the additional libraries that we want. And since virtual environments are created on the fly, we can create multiple different virtual environments. This means that different applications can use different virtual environments. In solving the problem above, we just create separate virtual environments for app A and app B and install the required packages separately.

The images below tries to show the situation with and without a virtual environment.

Without Virtual Environment

With Virtual Environment

III. What is virtualenv and how do I install it?

In this tutorial, we will be using virtualenv. This is a python package that is used to create isolated Python environments. In short, virtualenv is used to create a virtual environment for our Django project.

To install, open the command line and enter the commands below.

For windows:
$ pip install virtualenv
For Linux
$ [sudo] pip install virtualenv

If the same message like the one below appears, the installation was a success.

D:\test>pip install virtualenv
Collecting virtualenv
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

IV. How do I create a virtual environment using virtualenv?

  1. Create a directory or folder where you want to place your project. In our example, we would be placing it on drive D and name the folder 'test' (using Windows).
  2. Go inside the folder you just created and launch the command prompt from there. You can do this by holding shift + right click then clicking 'Open command window here'.
  3. Enter the command below to install. Note that the word 'env' will be used as the name of our environment. We can use any name as long as it is valid. Here, we just use the word 'env' which is short for environment.
    $ virtualenv env
    
  4. The message below will be shown if the creation is a success. We can also confirm it by seeing a folder with the same name of our created environment in the project folder.
Using base prefix 'c:\\users\\bryan\\appdata\\local\\programs\\python\\python36-32'
New python executable in D:\test\env\Scripts\python.exe
Installing setuptools, pip, wheel...done.

Note: Windows example is shown here
Folder created

Folder structure inside

V. How do I activate and deactivate the virtualenv?

While still on the directory where you've installed the environment, activate the virtualenv by using either of the commands below.

For linux:

$ source env/bin/activate

For Windows:

$ env/scripts/activate

We should see '(env)' at the command prompt, letting us know that we're running under the 'env' virtualenv install. Do not close the terminal once done. We need it activated so that our dependencies are installed in the virtual environment and not globally. So any time we install a library, it will only be applied on the virtual environment if the '(env)' is shown. If not shown, then it will install globally.

At any time, just enter the code below to stop using virtualenv.

(env) $ deactivate

Curriculum

This is just part one of a multi-series tutorial wherein we are going to build a full single page application and deploy it on a cloud server. Next, we will be installing Django using virtualenv and create our first 'Hello World' application.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hey @burdagay I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Looking forward to the series..
This will surely be a big help to everyone, including me, who are still trying to get started with Django/Python.

Glad to be of help. Cheers!

Thank you for the contribution. It has been approved.

Important Note: Normally, I wouldn't approve this. There are tons of other "Django Installation Tutorials". I accepted this because it's well written and the start of a curriculum. So, next time, please check out the topic you are going to write before doing it. So by that way, you can add differences to your post to avoid "similarities".

Good work, keep going.

You can contact us on Discord.
[utopian-moderator]

Thank you. I understand that there are a lot of Django tutorials. From now on, I will try to add something new to differentiate.