How To Install Python Django In Virtual Environment

When you develop a Python application, you may want to make your develop environment-used libraries independent from other libraries that the system provided or other projects used. And then create the project in that isolated environment, so that the project can use the library it needs only and each project will not influence each other. Python virtualenv module provides methods to help you achieve this. This example will just tell you how to install python’s virtual environment and Django server in that virtual environment.

1. Install Python virtualenv Module.

  1. Check whether the python module virtualenv has been installed or not by executing the command pip list or pip show virtualenv in a terminal. If the virtualenv module does not exist in the list, then you need to install it.
    $ pip list
    Package    Version
    ---------- ---------
    certifi    2019.6.16
    chardet    3.0.4
    Django     2.1.5
    idna       2.8
    pip        19.1.1
    pytz       2018.9
    requests   2.22.0
    setuptools 39.0.1
    urllib3    1.25.3
    virtualenv 16.4.0
    $ pip show virtualenv
    Name: virtualenv
    Version: 16.4.0
    Summary: Virtual Python Environment builder
    Author: Ian Bicking
    Author-email: [email protected]
    License: MIT
    Location: c:\users\zhaosong\appdata\roaming\python\python37\site-packages
  2. Run the command pip install –user virtualenv in terminal to install python virtualenv module.
    192:TodoList $ pip install --user virtualenv
    Collecting virtualenv
      Downloading (1.9MB)
        100% |████████████████████████████████| 1.9MB 2.8MB/s 
    Installing collected packages: virtualenv
      The script virtualenv is installed in '/Users/zhaosong/.local/bin' which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Successfully installed virtualenv-16.0.0

2. Create A Virtual Environment And Activate It.

  1. Create a directory and cd into it then run the below command in the terminal to create a python virtual environment. After that, you can find my_env folder has been created in the current directory.
    $ python -m venv my_env
  2. Before you can use the virtual environment, you need to activate it by running the activate executable file in the my_env/bin folder. Then you can dive into the virtual environment.
    192:virtualenv$ source my_env/bin/activate
    (my_env) 192:virtualenv $
  3. If you want to exit the virtual environment, run the deactivate command in the virtual environment terminal.
    (my_env) 192:virtualenv zhaosong$ deactivate

3. Install Django In Virtual Environment.

  1. Now the virtual environment has been created and activated, you can install any python library in it. Now we will install Django in it to show how to create a web application with Django.
  2. Before installing Django, please run pip list or pip show django in the Python virtual environment terminal, you can find that there does not has too much module has been installed.
  3. Now run pip install Django command in the virtual environment, it will download and install Django as below.
    (my_env) 192:virtualenv $ pip install Django
    Collecting Django
      Cache entry deserialization failed, entry ignored
      Downloading (7.3MB)
        100% |████████████████████████████████| 7.3MB 165kB/s 
    Collecting pytz (from Django)
      Downloading (510kB)
        100% |████████████████████████████████| 512kB 1.6MB/s 
    Installing collected packages: pytz, Django
    Successfully installed Django-2.1.1 pytz-2018.5
    You are using pip version 9.0.3, however version 18.0 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
  4. Run pip list again, it will list the installed python module.
  5. You can also run the command pip show Django to check whether the Python Django module has been installed successfully or not.

4. Create Django Project.

  1. After installing the virtual environment and the Django module, now we can create a Django project using the below command.
    (my_env) 192:virtualenv $ startproject my_django_project
  2. The above command will create a folder my_django_project, cd into this folder, you can find there is another same name folder and a file in it.
    │   db.sqlite3
  3. contains short python code which can accept command-line arguments to run tasks like start the Django server and working with SQLite database.
  4. contains configuration data that manage your project and controls how the Django server interacts with the system.
  5. controls request url and response function mappings.
  6. is the abbreviation of web server gateway interface, it will act as a helper of Django server to serve files in this project.

5. Create SQLite Database.

  1. All the project information is saved in an SQLite database in Django.
  2. So we need to use the below command to create a database for this project to use. After creation, the db.sqlite3 file will be created in the current folder.
    (my_env) 192:my_django_project $ python migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying admin.0003_logentry_add_action_flag_choices... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying sessions.0001_initial... OK
    (my_env) 192:my_django_project $ ls
    db.sqlite3 my_django_project

6. Start Django Server With The Project.

  1. CD into the project directory and start the Django web server with the below command. You can specify any port number as you like. And the default port number is 8000.
    (my_env) 192:my_django_project $ python runserver 8000
  2. Open a web browser and browse the url, then you can see the Django webserver project home page.

7. Administrate The Django Web Site.

  1. Now the Django website has been started, and there is also an admin site for it. Administrators can use the admin site to manage it.
  2. First, you should create a superuser as the website administrator in the terminal follow the below command.
    (my_env) 192:my_django_project $ python createsuperuser
    Username (leave blank to use 'zhaosong'): admin
    Email address: [email protected]
    Password (again): 
    The password is too similar to the email address.
    This password is too short. It must contain at least 8 characters.
    This password is too common.
    Bypass password validation and create user anyway? [y/N]: y
    Superuser created successfully.
  3. Now start the Django webserver if it does not run, otherwise go to step 3.
    (my_env) 192:my_django_project $ python runserver
  4. Open a web browser and browse URL http://localhost:8000/admin/, input the superuser username and password in step 1, click the Log in button to log in.
  5. Then the Django webserver admin dashboard will be shown. You can manage the site administration Groups and Users in it.

1 thought on “How To Install Python Django In Virtual Environment”

  1. Thanks for the tutorial (I can’t space for some reason) using firefox. Anyway, Step4 my django_admin is a .exe not .pyfile thanks again!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.