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

相关文章
|
2月前
|
PyTorch Linux 算法框架/工具
pytorch学习一:Anaconda下载、安装、配置环境变量。anaconda创建多版本python环境。安装 pytorch。
这篇文章是关于如何使用Anaconda进行Python环境管理,包括下载、安装、配置环境变量、创建多版本Python环境、安装PyTorch以及使用Jupyter Notebook的详细指南。
296 1
pytorch学习一:Anaconda下载、安装、配置环境变量。anaconda创建多版本python环境。安装 pytorch。
|
1月前
|
设计模式 前端开发 数据库
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
150 45
|
24天前
|
机器学习/深度学习 数据可视化 Docker
Python环境
Python环境
32 3
|
1月前
|
弹性计算 Linux iOS开发
Python 虚拟环境全解:轻松管理项目依赖
本文详细介绍了 Python 虚拟环境的概念、创建和使用方法,包括 `virtualenv` 和 `venv` 的使用,以及最佳实践和注意事项。通过虚拟环境,你可以轻松管理不同项目的依赖关系,避免版本冲突,提升开发效率。
68 3
|
1月前
|
安全 数据库 开发者
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
51 2
|
1月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
42 1
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
Python Web框架比较:Django vs Flask vs Pyramid
38 4
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第10天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django以功能全面、文档完善著称,适合快速开发;Flask轻量灵活,易于上手;Pyramid介于两者之间,兼顾灵活性和安全性。选择框架时需考虑项目需求和个人偏好。
38 1
|
2月前
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第6天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django功能全面,适合快速开发;Flask灵活轻量,易于上手;Pyramid介于两者之间,兼顾灵活性和可扩展性。文章分析了各框架的优缺点,帮助开发者根据项目需求和个人偏好做出合适的选择。
41 4
|
2月前
|
IDE 网络安全 开发工具
IDE之pycharm:专业版本连接远程服务器代码,并配置远程python环境解释器(亲测OK)。
本文介绍了如何在PyCharm专业版中连接远程服务器并配置远程Python环境解释器,以便在服务器上运行代码。
376 0
IDE之pycharm:专业版本连接远程服务器代码,并配置远程python环境解释器(亲测OK)。