Creating development environments and installing Django with Anaconda

Versão em português deste post / Portuguese version of this post

In today’s post, we’ll see how to create development environments and how to install Django using the Anaconda distribution.

In my Data Analysis and Data Science with Python posts, I always recommended the use of the Anaconda distribution to start working with the language. It is tested and used by thousands of people and it already comes with lots of important Python packages. Anaconda also comes with “conda”. “conda” works as an environment and package manager. Regarding environments, its functioning is similar to “virtualenv”. In this way, you can use it to create environments, where projects will have their own isolated configurations and packages installed, not messing with other projects setup. This is considered a good practice, because when you are working with more than one project, you can quickly start to have version conflicts between packages versions and Python’s version itself.

Regarding Anaconda installation, I already talked about it in this post, so, if you still don’t have it installed, it is pretty simple. Now, let’s talk about the creation of environments using “conda”, and after that, the installation of Django itself (which will also work for any other package, like Flask, for example).

Creating the environment

Let’s start creating our environment. After the installing, access your operational system’s terminal, type “conda” and hit “Enter”. The terminal will show how to use “conda”, with the commands and their uses. To start, you can list all your environments with the following command:

conda info --envs

You will see a list which will look like that (but will not be equal, since these are the environments I created):

# conda environments:
#
marketplacedigital       /home/felipe/anaconda3/envs/marketplacedigital
python2_env              /home/felipe/anaconda3/envs/python2_env
root                  *  /home/felipe/anaconda3

Now, let’s create a new environment. To create a new environment, we use the “conda create” command, with the –name argument, which will be the name of the environment, and we will also specify the Python version to be used on this environment. “conda” will show where it will put your environment, some basic packages that will be installed along with Python and will ask you to confirm the creation of the environment. Just type “y” and hit “Enter”:

conda create --name test_environment python=3
Fetching package metadata .......
Solving package specifications: ..........

Package plan for installation in environment /home/felipe/anaconda3/envs/test_environment

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    xz-5.2.2                   |                1         669 KB
    python-3.6.0               |                0        16.3 MB
    setuptools-27.2.0          |           py36_0         523 KB
    wheel-0.29.0               |           py36_0          88 KB
    pip-9.0.1                  |           py36_1         1.7 MB
    ------------------------------------------------------------
                                           Total:        19.2 MB

The following NEW packages will be INSTALLED:

    openssl:    1.0.2j-0     
    pip:        9.0.1-py36_1 
    python:     3.6.0-0      
    readline:   6.2-2        
    setuptools: 27.2.0-py36_0
    sqlite:     3.13.0-0     
    tk:         8.5.18-0     
    wheel:      0.29.0-py36_0
    xz:         5.2.2-1      
    zlib:       1.2.8-3      

Proceed ([y]/n)? y

Then, “conda” will create the environment, download the packages and install them. In the end, it will show you how to access the environment:

#
# To activate this environment, use:
# > source activate test_environment
#
# To deactivate this environment, use:
# > source deactivate test_environment
#

Let’s do it then. Let’s access the environment:

source activate test_environment

Now, your terminal will indicate the environment that you are right now. The only thing left to do is install “Django”. We can do it through “pip”, which is installed in your newly created environment:

pip install django
Collecting django
  Downloading Django-1.10.4-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 180kB/s 
Installing collected packages: django
Successfully installed django-1.10.4

Finally, you can confirm if everything is in place and that’s it. Django is installed in an isolated environment, which will have its own configuration. Beautiful!

python -c "import django; print(django.get_version())"
1.10.4

To get out of this environment (and activate other one, if you want), just type the following:

source deactivate

To finish it, if you want to remove your environment, use the “conda remove” command, with the name of the environment after the “–name” argument, just like we did when creating it, and the “–all” option in the end:

conda remove --name test_environment --all

Hope this is helpful 🙂

One comment on “Creating development environments and installing Django with Anaconda

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *