When you develop Python application, you may want to make you develop environment used libraries independent from other libraries that system provided or other projects used. And then create 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 provide methods to help you achieve this. This example will just tell you how to install python virtual environment and Django server in that virtual environment.
1. Install Python virtualenv Module.
- Check whether the python virtualenv module has been installed or not by execute below command in a terminal. If the virtualenv module do not exist in the list, then you need to install it.
- Run below command in terminal to install python virtualenv module.
192:TodoList $ pip install --user virtualenv Collecting virtualenv Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (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.
- Create a directory and cd into it then run below command in terminal to create a python virtual environment. After that you can find my_env folder has been created in current directory.
$ python -m venv my_env
- Before you can use the virtual environment, you need to activate it by run the activate executable file in my_env bin folder. Then you have dive into the virtual environment.
192:virtualenv$ source my_env/bin/activate (my_env) 192:virtualenv $
- If you want to exit the virtual environment, run deactivate command in the virtual environment terminal.
(my_env) 192:virtualenv zhaosong$ deactivate
3. Install Django In Virtual Environment.
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 web application with Django.
- Before install Django, please run
pip listin the python virtual environment terminal, you can find that there do not has too much module has been installed.
- Now run
pip install Djangocommand 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 https://files.pythonhosted.org/packages/ca/7e/fc068d164b32552ae3a8f8d5d0280c083f2e8d553e71ecacc21927564561/Django-2.1.1-py3-none-any.whl (7.3MB) 100% |████████████████████████████████| 7.3MB 165kB/s Collecting pytz (from Django) Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (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.
pip listagain, it will list the installed python module.
4. Create Django Project.
- After install virtual environment and the Django module, now we can create a Django project using below command.
(my_env) 192:virtualenv $ django-admin.py startproject my_django_project
- Above command will create a folder my_django_project, cd into this folder, you can find there is another same name folder and a manage.py file in it.
- manage.py : contains short python code which can accept command line arguments to run tasks like start the Django server and working with sqlite database.
- settings.py : contains configuration data which manage your project and control how the Django server interact with the system.
- urls.py : controls request url and response function mappings.
- wsgi.py : 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.
All the project information are saved in a SQLite database in Django, so we need to use below command to create a database for this project to use. After creation, db.sqlite3 file will be created in current folder.
(my_env) 192:my_django_project $ python manage.py 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 manage.py my_django_project
6. Start Django Server With The Project.
- CD into the project directory and start Django server with 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 manage.py runserver 8000
- Open a web browser and input url http://127.0.0.1:8000/, then you can see below web page.
7. Administrate The Django Web Site.
Now the Django web site has been started, and there is also an admin site for it. Administrator can use the admin site to manage it.
- First you should create a superuser as the web site administrator in terminal follow below command.
(my_env) 192:my_django_project $ python manage.py createsuperuser Username (leave blank to use 'zhaosong'): admin Email address: [email protected] Password: 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.
- Now start the Django web server if it do not run, otherwise go to step 3.
(my_env) 192:my_django_project $ python manage.py runserver
- Open a web browser and input http://localhost:8000/admin/, input the username and password in step 1, click login.
- Then the Django web server admin dashboard will be shown. You can manage user group and user information in it.