Setup Django environment with Anaconda

2022-04-10 12:00:29 浏览数 (1)

It is difficult to manage multiple python environments on a single machine, especially for environments that have different third party libraries to be installed.

As a developer, I am always mess up my development environments, and re installation of whole operating system takes too much time. I am using anaconda to manage my development environment.

Install anaconda

Install anaconda is very straightforward, you can just go ahead to download the installer from the anaconda official portal and run the installer.

Configure proxy for anaconda

Each user can have her own condarc file under the home folder. It is in my folder of /home/hui/.condarc

代码语言:javascript复制
channels:
  - conda-forge
  - defaults

proxy_servers:
    http: http://127.0.0.1:9090
    https: http://127.0.0.1:9090

ssl_verify: False

Create a dedicated environment for my Django framework

代码语言:javascript复制
conda create --name webenv3 python=3

My environment webenv3 is based on python 3 version. Of course, you can also create another python 2 version environment for your compatibility testing purpose.

代码语言:javascript复制
conda create --name webenv2 python=2

Environment switch

You can switch the two separated environments back and forth with conda commands.

代码语言:javascript复制
(webenv3) hui@hui-TM1701:~$ conda activate webenv2
(webenv2) hui@hui-TM1701:~$ conda activate webenv3
(webenv3) hui@hui-TM1701:~$ conda env list
# conda environments:
#
base                     /home/hui/anaconda3
webenv                   /home/hui/anaconda3/envs/webenv
webenv2                  /home/hui/anaconda3/envs/webenv2
webenv3               *  /home/hui/anaconda3/envs/webenv3

Django framework installation

代码语言:javascript复制
pip --proxy 127.0.0.1:9090 install djangopython -m django --version
2.2.3mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Migrate the database changes

代码语言:javascript复制
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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

Start the server

代码语言:javascript复制
python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 29, 2019 - 15:15:47
Django version 2.2.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Done!

0 人点赞