1.django 环境搭建
1.安装python
参考:
yum update -y
yum -y groupinstall "Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
cd /usr/local
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar -zxvf Python-3.6.6.tgz
cd Python-3.6.6
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
# 修改pip源
cd ~
mkdir .pip
cd .pip
vim pip.conf
#进入后添加以下内容,保存退出.
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
2.安装虚拟环境
pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
3.创建虚拟环境
[root@localhost /]# mkdir /django
[root@localhost /]# cd /django
[root@localhost django]# mkdir day01
[root@localhost django]# virtualenv env_py3 --python=/usr/bin/python3 # mkvirtualenv env_py3 -p /usr bin/python3
[root@localhost django]# cd env_py3/bin
[root@localhost bin]# source activate #启动虚拟环境
(env_py3) [root@localhost bin]# #成功进入虚拟环境
(env_py3) [root@localhost bin]# deactivate #退出虚拟环境
[root@localhost bin]#
4.虚拟环境安装django和uwsgi
(env_py3) [root@localhost bin]# pip3 install django==2.2 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
(env_py3) [root@localhost bin]# pip3 install django==1.11 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
#(如果用于生产的话,则需要指定安装和你项目相同的版本)
(env_py3) [root@localhost bin]# pip3 install uwsgi -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
5.创建项目
(env_py3) [root@localhost django]# cd day01/
(env_py3) [root@localhost day01]# tree
(env_py3) [root@localhost day01]# django-admin startproject HelloDjango
(env_py3) [root@localhost day01]# cd /django/day01/HelloDjango
(env_py3) [root@localhost day01]# python manage.py startapp App
#报错: 需要升级centos7自带得SQLite
#django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
(env_py3) [root@localhost day01]#
-------------------------------------------------------------------------------------
在 centos 7 上面运行 django 2.2 开发服务器时出现:
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
原因时系统自带 sqlite3 版本太低,解决方法是升级就可以了。
升级:
# 下载源码
wget https://www.sqlite.org/2019/sqlite-autoconf-3290000.tar.gz
# 编译
tar zxvf sqlite-autoconf-3290000.tar.gz
cd sqlite-autoconf-3290000/
./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
import sqlite3
sqlite3.sqlite_version
再次启动运行 django 2.2 开发服务器就 ok 了。
--------------------------------------------------------------------------------------
6.pycharm下载
Linux版下载地址:
https://download.jetbrains.8686c.com/python/pycharm-community-2019.3.4.tar.gz
[root@localhost local]# cd /usr/local
[root@localhost local]# tar -zxvf pycharm-community-2019.3.4.tar.gz
[root@localhost local]# cd /bin
[root@localhost local]# ./pycharm.sh
---图形化安装过程省略----
[root@localhost local]# ln -s /usr/local/pycharm-community-2019.3.4/bin/pycharm.sh /usr/local/bin/pycharm
7.pycharm打开工程
1.打开/data/projects/day01/HelloDjango,注意pycharm里面不能打开工程的上一级,
2.修改工程的python:
- 选择工程-文件-settings-Project(工程名称)-project interpreter
- 点击project interpreter右侧设置图标-show all - add - Virtual Environment - Exisiting Environment -选择我们创建的虚拟环境/data/envs/dj111/bin/python3,勾选make avaliable to all projects.-保存。
- 选择新建的interpreter,然后保存退出。
8.pycharm 安装database 插件
参考:https://blog.csdn.net/liushuijingying2/article/details/80694690
社区版没有数据库插件,可以安装Database Navigator插件
1、Pycharm内部设置安装: File →Settings→Plugins→搜索框中输入Database Navigator→下载插件→安装完成重启Pycharm。
2、手动安装: 打开PyCharm Plugins官网并搜索Database Navigator→确认安装的Pycharm版本→下载版本对应的Database Navigator压缩包→在Pycharm中安装压缩包→重启Pycharm。
https://plugins.jetbrains.com/pycharm
9.配置mysql连接
#修改项目下的settings.py
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dj111',
'USER': 'root',
'PASSWORD':'Ora123456',
}
}
#修改项目的__init__.py添加:
import pymysql
pymysql.install_as_MySQLdb() #python2.x不需要伪装 python3.x需要伪装
#安装驱动pymysql
(env_py3) [root@localhost day01]# pip install pymysql -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
#执行迁移
(env_py3) [root@localhost day01]# python manage.py migrate
# 下载Python3.9.5
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz
tar zxvf Python-3.9.5.tgz
# 安装必须的包
若未安装dnf,安装dnf命令如下:
前置依赖:
yum install epel-release 或者 yum install epel-release -y
安装dnf:
yum install dnf
dnf -y install gcc zlib* libffi-devel
cd Python-3.9.5/
#注意:prefix=/usr/local/python3是指定的python3.9安装目录,可设置成自己想要放的目录
./configure --prefix=/usr/local/python3 --enable-optimizations
make
make install
# 删除原先的Python3和pip3 (让系统使用最新版本的python)
rm -rf /usr/bin/python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
rm -rf /usr/bin/pip3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3