【踩坑】linux安装配置django环境踩坑记
目录:
1:linux用django startproject报错
2:python manage.py runserver报错:SQLite 3.8.3 or later is required (found 3.7.17)
3:无法直接使用pip3或pip命令
4:安装uwsgi报错:plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
5:安装uwsgi报错:ModuleNotFoundError: No module named '_ctypes'
6:uwsgi启动报错:probably another instance of uWSGI is running on the same address
7:安装uwsgi报错:Command "/usr/bin/python3 -u -c "import setuptools, tokenize;file='/tmp/pip-build-zcnqe9e2/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-f2pzir0h-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-zcnqe9e2/uwsgi/
8:域名/admin报错:Invalid block tag on line 23: 'translate'
坑1:linux用django startproject报错👇
[root@iZo4t6je6mwbb8Z django]# django-admin.py startproject demo1
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/bin/django-admin.py", line 5, in <module>
from django.core import management
ImportError: No module named django.core
原因:
linux默认是有python2的,但我又安装了python3,django是在python3的lib里,所以执行django-admin.py startproject learn,调用的python环境是python2的,所以找不到core.management。
解决方案:
cd /usr/bin
mv python python2 #原先的python是python2.7的软连接
mv python3 python #现在修改python为python3
实际演示:
[root@iZo4t6je6mwbb8Z django]# cd /usr/bin
[root@iZo4t6je6mwbb8Z bin]# mv python python2
mv: overwrite ‘python2’? y
[root@iZo4t6je6mwbb8Z bin]# mv python3 python
[root@iZo4t6je6mwbb8Z bin]# cd /home/liujia/django
[root@iZo4t6je6mwbb8Z django]# django-admin.py startproject demo1
[root@iZo4t6je6mwbb8Z django]# ls
demo1
[root@iZo4t6je6mwbb8Z django]# cd demo1
[root@iZo4t6je6mwbb8Z demo1]# ls
demo1 manage.py
[root@iZo4t6je6mwbb8Z demo1]#
坑2:python manage.py runserver报错:SQLite 3.8.3 or later is required (found 3.7.17)
原因:
阿里云自带的centos的sqlite版本低了
解决方案:
#更新SQLite 3
#获取源代码(在主目录中运行)
[root@djangoServer ~]# cd ~
[root@djangoServer ~]# wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
[root@djangoServer ~]# tar -zxvf sqlite-autoconf-3270200.tar.gz
#构建并安装
[root@djangoServer ~]# cd sqlite-autoconf-3270200
[root@djangoServer sqlite-autoconf-3270200]# ./configure --prefix=/usr/local
[root@djangoServer sqlite-autoconf-3270200]# make && make install
[root@djangoServer sqlite-autoconf-3270200]# find /usr/ -name sqlite3
/usr/bin/sqlite3
/usr/lib64/python2.7/sqlite3
/usr/local/bin/sqlite3
/usr/local/python3/lib/python3.7/site-packages/django/db/backends/sqlite3
/usr/local/python3/lib/python3.7/sqlite3
[root@djangoServer sqlite-autoconf-3270200]#
#不必要的文件,目录删除
[root@djangoServer sqlite-autoconf-3270200]# cd ~
[root@djangoServer ~]# ls
anaconda-ks.cfg sqlite-autoconf-3270200 sqlite-autoconf-3270200.tar.gz
[root@djangoServer ~]#
[root@djangoServer ~]# rm -rf sqlite-autoconf-3270200.tar.gz
[root@djangoServer ~]# rm -rf sqlite-autoconf-3270200
#检查版本
## 最新安装的sqlite3版本
[root@djangoServer ~]# /usr/local/bin/sqlite3 --version
3.27.2 2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0f6d7
[root@djangoServer ~]#
## Centos7自带的sqlite3版本
[root@djangoServer ~]# /usr/bin/sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668
[root@djangoServer ~]#
## 可以看到sqlite3的版本还是旧版本,那么需要更新一下。
[root@djangoServer ~]# sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668
[root@djangoServer ~]#
## 更改旧的sqlite3
[root@djangoServer ~]# mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
## 软链接将新的sqlite3设置到/usr/bin目录下
[root@djangoServer ~]# ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
## 查看当前全局sqlite3的版本
[root@djangoServer ~]# sqlite3 --version
3.27.2 2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0f6d7
[root@djangoServer ~]#
#将路径传递给共享库
# 设置开机自启动执行,可以将下面的export语句写入 ~/.bashrc 文件中,如果如果你想立即生效,可以执行source 〜/.bashrc 将在每次启动终端时执行
[root@djangoServer ~]# export LD_LIBRARY_PATH="/usr/local/lib"
#检查Python的SQLite3版本
[root@djangoServer ~]# ipython3
Python 3.7.1 (default, May 3 2019, 09:55:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import sqlite3
In [2]: sqlite3.sqlite_version
Out[2]: '3.27.2'
In [3]: exit
[root@djangoServer ~]#
#启动开发服务器
[root@djangoServer ~]# cd /work/
[root@djangoServer work]# ls
db.sqlite3 manage.py polls test_django
[root@djangoServer work]# python3 manage.py runserver
坑3:无法直接使用pip3或pip命令
原因:
未知
解决方案1:
使用python -m pip代替
解决方案2:另一个怀疑能解决的方案是安装libffi-devel,然后重新从./configure这一步开始重新安装python
yum install libffi-devel
坑4:安装uwsgi报错:plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
原因:
未知
解决方案:
yum install python-devel.x86_64
坑5:安装uwsgi报错:ModuleNotFoundError: No module named '_ctypes'
yum install libffi-devel
从./configure这一步开始重新安装python
坑6:uwsgi启动报错:probably another instance of uWSGI is running on the same address
可以用命令杀掉这个端口在重启:
sudo fuser -k 8080/tcp
坑7:安装uwsgi报错:Command "/usr/bin/python3 -u -c "import setuptools, tokenize;file='/tmp/pip-build-zcnqe9e2/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-f2pzir0h-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-zcnqe9e2/uwsgi/
yum install python3-devel -y
坑8:Invalid block tag on line 23: 'translate'
vi 到具体文件,执行替换命令:
:%s#translate#trans#g