0%

Python Develop Tips

1、使用pip3 安装Python3 的资源库

pip3 install xlwt
或者想要一次性安装多个库需要把需要安装的库放在requirements.txt,使用pip3安装的可以使用指令
pip3 install -r requirements.txt

2、python 合并拼接字符串

1
2
3
4
5
6
7
8
9
>>> a = ["hello", "world"]
>>> a
['hello', 'world']
>>> ' '.join(a)
'hello world'
>>> ','.join(a)
'hello,world'
>>> ''.join(a)
'helloworld'

3、使用pip相关

1
2
使用pip3 list 查看当前已安装的库和版本
使用 pip install --upgrade pip 进行升级

4、VSCode 过滤.pyc等文件,在settings.json输入

1
2
3
4
5
6
7
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/*.pyc": true,
"**/.DS_Store": true
}

5、创建Django项目

1
2
3
4
# 创建Django项目
django-admin startproject name
# 创建子应用
python manage.py startapp name

6、模型迁移

迁移由两步完成 :

  • 生成迁移文件,根据模型类生成创建表的语句
    python manage.py makemigrations
  • 执行迁移:根据第一步生成的语句在数据库中创建表
    python manage.py migrate

7、Django继承AbstractUser新建User Model时出现错误

ERRORS:

1
2
3
4
5
6
7
8
9

audit.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
audit.UserProfile.user_permissions: (fields.E304) Reverse accessor for 'UserProfile.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'UserProfile.user_permissions' or 'User.user_permissions'.
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserProfile.user_permissions'.

需要在setting中重载AUTH_USER_MODEL

1
2
3
4
5
AUTH_USER_MODEL = 'users.UserProfile'

users:你的app

UserProfile:model

8、Django创建管理用户

python manage.py createsuperuser

9、RuntimeError: cryptography is required for sha256_password or caching_sha2_password

这段报错意思是说 sha256_password 和 caching_sha2_password 这两个加密算法需要用到 cryptography 。虽然意思很明了,但是可能不知道如何解决。
其实 cryptography 是一个python包,所以解决方法很简单:

1
pip3 install cryptography

10 、NameError: name ‘_mysql’ is not defined

解决Django执行manage.py 提示 NameError: name ‘_mysql’ is not defined 问题
原因是:
Mysqldb 不兼容 python3.5 以后的版本

解决办法:
使用pymysql代替MySQLdb

步骤:

安装pymysql:pip install pymysql
打开项目在setting.py的init.py,或直接在当前py文件最开头添加如下:

1
2
import pymysql 
pymysql.install_as_MySQLdb()

重新执行后报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.1.
有一个好办法,直接指定版本,比其他的解决方法简单一些…

1
2
3
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()

参考

11、Error: That port is already in use.

有两个方法可以解决(假设8000端口被占用):
1.使用python manage.py runserver 8001 开一个新的端口。
2.kill掉原来的端口(在root条件下)。
2.1 在终端输入lsof -i:8000,列出进程信息。
2.2 然后,找到进程的PID号,比如我的PID号就是24194。
2.3 输入kill -9 PID,比如kill -9 24194 就可以关闭该端口了。
2.4使用python manage.py runserver 就能继续使用8000端口了。