Python

安装python3.9

操作系统版本是CentOS7.4,OpenSSL 1.0.2u。由于python3.10需要openssl 1.1.1 or newer,为了减少麻烦,安装python 3.9。

安装相关包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make  libffi-devel

下载安装包。内网服务器,使用了代理。

#下载Python3.9,并安装
wget -e https_proxy=192.168.0.100:8080 https://www.python.org/ftp/python/3.9.12/Python-3.9.12.tar.xz
xz -d Python-3.9.12.tar.xz
tar -xvf   Python-3.9.12.tar
cd  Python-3.9.12
./configure prefix=/usr/local/python3.9
make
make install

#配置路径
vi .bash_profile      
PATH=$PATH:$HOME/bin:/usr/local/python3.9/bin
export PATH

虚拟环境

1.创建虚拟环境

创建和管理虚拟环境的模块叫venv。首先决定存放虚拟环境的目录,然后进入该目录,运行venv模块:

python -m venv tutorial-env

这个命令在当前目录下创建tutorial-env子目录,同时在其下创建多个目录,存放python解析器及支持文件。

创建虚拟环境以后,就可以激活虚拟环境:

tutorial-env\Scripts\activaye.bat   #windows
source tutorial-env/bin/activate    #linux

举例:

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
  ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

取消激活虚拟环境,输入命令:

deactivate

2.使用pip管理包

(tutorial-env) $ python -m pip install novas  #安装最新包
(tutorial-env) $ python -m pip install requests==2.6.0 #安装指定版本包
(tutorial-env) $ python -m pip install --upgrade requests #升级到最新版本
(tutorial-env) $ python -m pip show requests #显示指定包信息
(tutorial-env) $ python -m pip list  #显示所有包信息
(tutorial-env) $ python -m pip freeze > requirements.txt
(tutorial-env) $ python -m pip install -r requirements.txt


Django

出错处理

Django REST framework

  1. 出错信息:ImportError: cannot import name 'url' from 'django.conf.urls'

django在4.0已经废弃了django.conf.urls.url()

(django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+.)

在urls.py里面,用

from django.urls import re_path as url

代替

from django.conf.urls import url


  1. 启动时,Invalid HTTP_HOST header: '

修改创建项目时生成的setting.py文件

将ALLOWED_HOSTS = []改为ALLOWED_HOSTS = ['*']


批量导入数据

批量导入数据时,运行导入脚本,提示出错信息:

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

解决办法: 将

import os,django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'first_django_project.settings')
django.setup()

移到到了from first_app.models import *之前


安装记录

安装Django


#升级pip,安装django
/usr/local/python3.9/bin/python3.9 -m pip install --upgrade pip --proxy 192.168.0.100:8080
pip3 --proxy 192.168.0.100:8080  install django --index https://pypi.tuna.tsinghua.edu.cn/simple

#安装Django REST Framework
pip3 --proxy 192.168.0.100:8080  install djangorestframework -i https://pypi.tuna.tsinghua.edu.cn/simple  
pip3 --proxy 192.168.0.100:8080  install django-filter -i https://pypi.tuna.tsinghua.edu.cn/simple


升级SQLite

运行#python3 manage.py runserver  出错,错误信息最底一行

django.db.utils.NotSupportedError: deterministic=True requires SQLite 3.8.3 or higher

需要升级SQLite

#下载3.39.3版
wget -e https_proxy=192.168.0.100:8080 https://www.sqlite.org/2022/sqlite-autoconf-3390300.tar.gz

# 编译
tar zxvf sqlite-autoconf-339300.tar.gz
cd sqlite-autoconf-3339300
./configure --prefix=/usr/local
make && make install

# 替换系统低版本 sqlite3
mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3
echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
sqlite3 -version

升级到3.39.3,依然报上述错误,只能采用下述办法:

#安装pysqlite3
pip3 --proxy 192.168.0.100:8080 install pysqlite3 index https://pypi.tuna.tsinghua.edu.cn/simple  
pip3 --proxy 192.168.0.100:8080 install pysqlite3-binary  index https://pypi.tuna.tsinghua.edu.cn/simple 

#修改文件
vi /usr/local/python3.9.9/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py

#from sqlite3 import dbapi2 as Database  #注释它
from pysqlite3 import dbapi2 as Database #新加这段代码

 启动正常