Python常见问题及解决方法
一、Python3安装
1. centos下安装python
yum -y install epel-release
yum -y update
yum -y install python3.10
二、-bash: python3: command not found解决办法
根据python3的版本建立软链接:
ln -s /usr/bin/python3.10 /usr/bin/python3
删除python3软链接:
rm -rf /usr/bin/python3
三、卸载Python3
yum remove python3.*
以卸载python3.11为例:
yum remove python3.11
四、查找python版本
find /usr/bin/ -iname 'python*' -ls
五、第一个python程序
python3
>>> print("Hello World!")
六、运行python文件
cd *.py文件所在目录
python3 *.py
七、创建Django项目
django-admin startproject 项目名
八、-bash: django-admin: command not found解决办法
find / -name django-admin -print
ln -s 路径 /usr/bin/django-admin
九、在Django项目下创建应用
django-admin startapp 应用名
十、ModuleNotFoundError: No module named 'django'解决办法
给项目安装Django模块
十一、设置os.path后出现Internal server error解决办法
import os
十二、Django如何调用静态资源
修改settings.py配置静态资源
十三、配置static后图片不显示的解决办法
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import staticfiles
urlpatterns += staticfiles_urlpatterns()
十四、解决跨域Cross-Origin Opener Policy解决办法
SECURE_CROSS_ORIGIN_OPENER_POLICY = None
十五、CDN加载fontawesome出现Failed to decode downloaded font的原因
可能是网络导致的影响
十六、name 'settings' is not defined解决办法
from django.conf import settings
十七、name 'random' is not defined解决办法
import random
十八、module 'urllib' has no attribute 'request'解决办法
import urllib.request
十九、request.get_host()获取不到域名的解决办法
修改服务器配置文件:proxy_set_header Host 域名
二十、File "manage.py", line 17 ) from exc ^ 解决办法
是因为Python是2.*导致的,需要使用Python3
二十一、django.db.utils.NotSupportedError: SQLite * or later is required的解决办法
未安装SQLite或SQLite版本较低
查看sqlite版本命令:sqlite3 --version
二十二、如何安装SQLite
安装sqlite命令:yum install sqlite
二十三、如何升级SQLite
1. 下载和解压
wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
tar -zxvf sqlite-autoconf-3290000.tar.gz
2. 安装
cd sqlite-autoconf-3290000
./configure --prefix=/usr/local/sqlite
make && make install
3. 检查最新安装的版本
/usr/local/sqlite/bin/sqlite3 --version
4. 删除用过的文件
rm -rf sqlite-autoconf-3290000*
5. 备份旧的sqlite3
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
6. 软链接将新的sqlite3设置到/usr/bin目录下
ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3
7. 查看当前全局sqlite3的版本
sqlite3 --version
8. 配置lib库(重要)
创建/ect/ld.so.conf.d/sqlite3文件,内容为:/usr/local/sqlite/lib
执行ldconfig
二十四、django.db.utils.NotSupportedError: SQLite 3.27 or later is required (found 3.26.0).
sqlite已经升到了3.29,但是依然报错
通过rpm -qa | grep sqlite发现了3个3.26版本的安装
使用yum remove sqlite卸载了2个,还有1个无法卸载:
sqlite-libs-3.26.0-18.el8.x86_64
使用yum remove sqlite-lib*提示The operation would result in removing the following protected packages
rpm -e sqlite-libs-3.26.0-18.el8.x86_64 --nodeps搞定
二十五、django设置管理员
python manage.py migrate
python manage.py createsuperuser
二十六、django admin模板目录
/lib/python3.10/site-packages/django/contrib/admin/templates/admin
二十七、django登录跨域问题
CSRF_TRUSTED_ORIGINS = ['http://my.zhijiaoshe.cn',]