python virtualenv环境运行django (转)

原文链接:www.cnblogs.com/rwxwsblog/p/5711344.html

安装前准备

检查pip版本与python版本是否一致

[root@localhost bin]# whereis pip 
pip:/usr/bin/pip/usr/bin/pip2.7
如果版本不一致,修改pip引用的版本
[root@localhost bin]# mv /usr/bin/pip /usr/bin/pip2.7.2
[root@localhost bin]# ln -s /usr/local/python3.5.2/bin/pip3.5 /usr/bin/pip

安装virtualenv

[root@localhost bin]# pip install virtualenv 
Collecting virtualenv  
    Downloading virtualenv -15.0.2-py2. py3-none-any.whl (1.8MB)
    100% |████████████████████████████████|1.8MB 465kB/s
Installing collected packages: virtualenv 
Successfully installed virtualenv -15.0.2
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

创建运行环境

[root@localhost bin]# virtualenv ~/pythonproject/virtualenv/blog --no-site-packages
Using base prefix'/usr/local/python3.5.2'
New python executable in/root/pythonproject/virtualenv/blog/bin/python3.5
Also creating executable in/root/pythonproject/virtualenv/blog/bin/python 
Installing setuptools, pip, wheel...done.

指定Python版本命令:virtualenv --no-site-packages myblog --python=python3.6

启动virtualenv

source bin/activate

安装django

安装django

[root@localhost bin]# pip install django
Collecting django  
Downloading Django-1.9.8-py2.py3-none-any.whl (6.6MB)
100% |████████████████████████████████|6.6MB 70kB/s
Installing collected packages: django
Successfully installed django-1.9.8

创建django应用

(blog) [root@localhost blog]# cd ~
(blog) [root@localhost ~]# cd blog-bash: cd: blog: No such fileordirectory
(blog) [root@localhost ~]# mkdir django
(blog) [root@localhost ~]# cd django/
(blog) [root@localhost django]# django-admin startproject blog
(blog) [root@localhost django]# cd blog/
(blog) [root@localhost blog]# tree
.
├── blog
│  ├── __init__.py
│  ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── settings.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py2directories,7files

运行django应用

(blog)[root@localhost blog]#python3 manage.py runserver

报这样一个异常

Unhandled exceptioninthread startedby.wrapper at0x7f45d54ba488>
Traceback (most recentcalllast):  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line34,infrompysqlite2 import dbapi2asDatabaseImportError: Nomodule named'py sqlite2 '
During handlingofthe above exception, another exception occurred:
Traceback (most recentcalllast):  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line36,infromsqlite3 import dbapi2asDatabase  File"/usr/local/python3.5.2/lib/python3.5/sqlite3/__init__.py", line23,infromsqlite3.dbapi2 import *  File"/usr/local/python3.5.2/lib/python3.5/sqlite3/dbapi2.py", line27,infrom_sqlite3 import *
ImportError: Nomodulenamed'_sqlite3'During handlingofthe above exception, another exception occurred:Traceback (most recentcalllast):  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line226,inwrapper    fn(*args, **kwargs)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line109,ininner_run    autoreload.raise_last_exception()  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line249,inraise_last_exception    six.reraise(*_exception)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/six.py", line685,inreraise    raise value.with_traceback(tb)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line226,inwrapper    fn(*args, **kwargs)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/__init__.py", line18,insetup    apps.populate(settings.INSTALLED_APPS)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/registry.py", line108,inpopulate    app_config.import_models(all_models)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/config.py", line202,inimport_models    self.models_module = import_module(models_module_name)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line126,inimport_modulereturn_bootstrap._gcd_import(name[level:], package, level)  File"", line986,in_gcd_import  File"", line969,in_find_and_load  File"", line958,in_find_and_load_unlocked  File"", line673,in_load_unlocked  File"", line665,inexec_module  File"", line222,in_call_with_frames_removed  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/models.py", line4,infromdjango.contrib.auth.base_user import AbstractBaseUser, BaseUserManager  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line49,inclassAbstractBaseUser(models.Model):  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/base.py", line108,in__new__    new_class.add_to_class('_meta', Options(meta, app_label))File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/base.py", line307,inadd_to_class    value.contribute_to_class(cls, name)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/options.py", line263,incontribute_to_class    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/__init__.py", line36,in__getattr__returngetattr(connections[DEFAULT_DB_ALIAS], item)  File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line212,in__getitem__    backend = load_backend(db['ENGINE'])File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line116,inload_backendreturnimport_module('%s.base' % backend_name)File"/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line126,inimport_modulereturn_bootstrap._gcd_import(name[level:], package, level)  
File"/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line39,in    raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s"% exc)django.core.exceptions.ImproperlyConfigured:Errorloading either pysqlite2orsqlite3 modules (triedinthatorder): Nomodulenamed'_sqlite3'

这是由于没有安装sqlite-devel所致。
安装sqlite-devel: yum -y install sqlite-devel
cd ~/Python-3.5.2
编译: ./configure --prefix=/usr/local/python3.5.2
安装: make && make install
再次运行

(blog) [root@localhost blog]# python manage.py runserver
Performingsystemchecks...System check identifiednoissues (0silenced).You have unapplied migrations; your app maynotwork properlyuntilthey are applied.Run'python manage.py migrate'to apply them.July27,2016-13:47:44Django version1.9.8, using settings'blog.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

此时通过127.0.0.1可以访问,如果想非本机访问,可以在启动时添加相应的参数。如:
python manage.py runserver0.0.0.0:8000
此时通过http://ip:8000能正常访问了。

django配置

数据库初始化

python manage3.py migrate

创建超级用户,用于登录后台

(blog) [root@localhost blog]# python manage.py createsuperuser
Username (leave blank touse 'root'): root
Email address: [email protected]
Password:
Password(again):
Superuser created successfully.

此处账号为root,密码为:root1234。
此时可以通过,http://ip:8000/admin/访问后台。输入刚刚创建的用户名和密码即可成功登陆。
[图片上传中。。。(1)]

你可能感兴趣的:(python virtualenv环境运行django (转))