python virtualenv环境运行django

翻译 2018年04月17日 10:57:31

安装前准备

检查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/ 访问后台。输入刚刚创建的用户名和密码即可成功登陆。

virtualenv命令行创建django项目

使用pycharm社区版本开发工具不提供图形化创建django的方式,因此只能在命令行中进行创建。 1.安装virtualenv   pip install virtualenv 2...
  • u010381707
  • u010381707
  • 2016-08-28 18:01:48
  • 1228

python+django+uwsgi+nginx+virtualenv 部署高可用的python web环境

【摘要】  最近开始部署python的web环境,说实话对于python的单一站点部署还是有经验的,毕竟带团队已经多年(吹一下NB)。那么问题来了,不同版本的python环境部署还是必须了解的。今天...
  • caotianyin
  • caotianyin
  • 2017-05-23 11:34:54
  • 1769

[virtualenv]生产环境中使用virtualenv

virtualenv 对于python开发和部署都是好工具,可以隔离多个python版本和第三方库的版本,这里作者总结了几个常用python服务怎么样结合virtual部署 原文链接 Python 中...
  • lzz957748332
  • lzz957748332
  • 2015-04-15 22:35:56
  • 2717

在pythonanywhere上搭建django程序(Virtualenv+python2.7+django1.7+)

一天时间就用来鼓捣这个了,不过很开心终于成功了,昨天晚上折腾那个openshift到两点都没弄好(:з」∠) 项目本体是学校图书馆的第三方功能接口,本来是给自己工作的项目组做的api,做到现在差不多图...
  • zhu_free
  • zhu_free
  • 2015-05-30 23:28:19
  • 1816

django开发环境部署(一)——pip、virtualenv、virtualenvwrapper

Django 开发环境部署(一) 之所以写这篇文章的原因在于django环境的确轻松搭建,之前Ubuntu上安装了,的确很轻松,但是后期我才知道随便做个环境出来很容易到了后面很麻烦,污染了系统里...
  • sunmantan007
  • sunmantan007
  • 2014-10-09 00:23:08
  • 726

Python 之 沙盒环境--virtualenv

VirtualEnv用于在一台机器上创建多个独立的Python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。 使用 VirtualEnv 的理由: 隔离项目之间...
  • VIP_WangSai
  • VIP_WangSai
  • 2016-08-06 22:45:36
  • 1776

java调用python指定virtualenv虚拟环境下才能运行的python文件

有时候我们想要通过java调用一个python文件,而这个python文件又必须在特定的virtualenv虚拟环境下才能运行。如果直接将这个python文件所需要的包安装到python系统环境下难免...
  • willbkimps
  • willbkimps
  • 2017-04-22 21:37:04
  • 561

Centos7 + Python3.6 + Django + virtualenv + gunicorn + supervisor 环境配置详解

跟着网上的教程走发现行不通阿!好多都是写个大概,而且每人的环境都是有些许差异的,比如说权限问题阿,等等都会造成安装的失败 说明:本教程在你已经拥有Centos7系统,已经安装好nginx服务器,已经安...
  • m0_37687051
  • m0_37687051
  • 2017-07-17 20:25:17
  • 1185

django virtualenv 创建虚拟环境

python 开源社区很大, 所以第三方的包也不少, 有时候一台电脑上或者服务器上面可能运行不止一个django项目(project) 而对pyhon包的支持也有所不同,所以virtualenv  的...
  • u012687934
  • u012687934
  • 2013-11-02 21:07:51
  • 4146

virtualenv 打包在其他服务器环境配置的方法

要在n多服务器端部署python的应用,虽然python本身是跨平台的,当时好多第三方的扩展却不一定都能做到各个版本兼容,即便是都是linux,在redhat系列和ubuntu系列之间来回导也是个很让...
  • yuechuzhao
  • yuechuzhao
  • 2014-02-11 15:53:36
  • 9327
收藏助手
不良信息举报
您举报文章:python virtualenv环境运行django
举报原因:
原因补充:

(最多只允许输入30个字)