python virtualenv环境运行django

简介: python virtualenv环境运行django安装前准备检查pip版本与python版本是否一致[root@localhost bin]# whereis pippip: /usr/bin/pip /usr/bin/pip2.

python virtualenv环境运行django

安装前准备

检查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]# /usr/local/python3.5.2/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.

启动virtualenv

source 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 file or directory
(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.py

2 directories, 7 files

运行django应用

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

报这样一个异常

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f45d54ba488>
Traceback (most recent call last):
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 34, in <module>
    from pysqlite2 import dbapi2 as Database
ImportError: No module named 'pysqlite2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 36, in <module>
    from sqlite3 import dbapi2 as Database
  File "/usr/local/python3.5.2/lib/python3.5/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/python3.5.2/lib/python3.5/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: No module named '_sqlite3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/base.py", line 108, 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", line 307, in add_to_class
    value.contribute_to_class(cls, name)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/models/options.py", line 263, in contribute_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", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line 212, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/utils.py", line 116, in load_backend
    return import_module('%s.base' % backend_name)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/root/pythonproject/virtualenv/blog/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 39, in <module>
    raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_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
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

July 27, 2016 - 13:47:44
Django version 1.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 runserver 0.0.0.0:8000

此时通过http://ip:8000能正常访问了。

django配置

数据库初始化

python manage.py migrate

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

(blog) [root@localhost blog]# python manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: test@test.com
Password:
Password (again):
Superuser created successfully.

此处账号为root,密码为:root1234。
此时可以通过,http://ip:8000/admin/ 访问后台。输入刚刚创建的用户名和密码即可成功登陆。
img_4cda22b74cc22c5d928322b053f227ef.png

相关文章
|
15天前
|
存储 数据库连接 API
Python环境变量在开发和运行Python应用程序时起着重要的作用
Python环境变量在开发和运行Python应用程序时起着重要的作用
61 15
|
2月前
|
PyTorch Linux 算法框架/工具
pytorch学习一:Anaconda下载、安装、配置环境变量。anaconda创建多版本python环境。安装 pytorch。
这篇文章是关于如何使用Anaconda进行Python环境管理,包括下载、安装、配置环境变量、创建多版本Python环境、安装PyTorch以及使用Jupyter Notebook的详细指南。
279 1
pytorch学习一:Anaconda下载、安装、配置环境变量。anaconda创建多版本python环境。安装 pytorch。
|
27天前
|
设计模式 前端开发 数据库
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
135 45
|
16天前
|
机器学习/深度学习 数据可视化 Docker
Python环境
Python环境
26 3
|
26天前
|
弹性计算 Linux iOS开发
Python 虚拟环境全解:轻松管理项目依赖
本文详细介绍了 Python 虚拟环境的概念、创建和使用方法,包括 `virtualenv` 和 `venv` 的使用,以及最佳实践和注意事项。通过虚拟环境,你可以轻松管理不同项目的依赖关系,避免版本冲突,提升开发效率。
|
28天前
|
安全 数据库 开发者
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
41 2
|
29天前
|
算法 测试技术 开发者
在Python开发中,性能优化和代码审查至关重要。性能优化通过改进代码结构和算法提高程序运行速度,减少资源消耗
在Python开发中,性能优化和代码审查至关重要。性能优化通过改进代码结构和算法提高程序运行速度,减少资源消耗;代码审查通过检查源代码发现潜在问题,提高代码质量和团队协作效率。本文介绍了一些实用的技巧和工具,帮助开发者提升开发效率。
37 3
|
1月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
40 1
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
34 4
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第10天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django以功能全面、文档完善著称,适合快速开发;Flask轻量灵活,易于上手;Pyramid介于两者之间,兼顾灵活性和安全性。选择框架时需考虑项目需求和个人偏好。
37 1