Python——django的环境搭建sentOS、Ubuntu、widow10
搭建虚拟环境(sentOS)
安装python3(已安装可以跳过)
-
下载python3 安装包
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
-
安装依赖环境
yum -y install gcc gcc-c++ make yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel zlib libffi-devel kernel-devel kenel-headers bzip2 yum -y install libffi-devel
-
安装python3 包
tar -xf Python-3.8.5.tgz cd Python-3.8.5 ./configure --prefix=/usr/local/python3 make && install make
-
创建软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
-
安装成功测试
[root@mysql ~]# pip3 -v Usage: pip3 <command> [options] . . . [root@mysql ~]# python3 Python 3.8.5 (default, Aug 9 2020, 10:48:40) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
搭建虚拟环境
-
升级pip
/usr/local/python3/bin/python3.8 -m pip install -upgrade pip
-
安装 虚拟环境
sudo pip3 install virtualenv virtualenvwrapper
-
创建目录用来存放虚拟环境
mkdir ~/.virtualenvs -
打开 ~/.bashrc 文件,并添加如下:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh -
运行
source ~/.bashrc -
建立 软连接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
-
创建文件目录用于存放env和网站文件
mkdir -p /opt/env mkdir -p /opt/wwwroot
-
切换到/opt/env/下,创建指定版本的虚拟环境
virtualenv --python=/usr/local/python3/bin/python3 pyweb
-
切换到/opt/env/pyweb/bin 下安装django和uWSGI
pip3 install django pip3 install uwsgi
uWSGI是实现了了uwsgi和WSGI两种协议的Web服务器器。
-
创建django-admin.py软连接,并x执行授权
ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin.py chmod +x /usr/bin/django-admin.py
创建项目
-
切换到网站目录
/opt/wwwroot
创建Django项目django-admin.py startproject blog
-
切换到项目
/opt/wwwroot/blog
创建一个apppython3 manage.py startapp demo
如果提示:
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
则需要更新SQLite 3.8.3wget https://www.sqlite.org/snapshot/sqlite-snapshot-202008071952.tar.gz cd sqlite-snapshot-202008071952/ ./configure --prefix=/usr/local/sqlite3 make && make install
将/usr/lib64/目录下的libsqlite3 删除 替换为/usr/local/sqlite3/lib目录下的libsqlite3
[root@localhost blog]# find / -name *libsqlite3.so* /usr/lib64/libsqlite3.so.0 /usr/lib64/libsqlite3.so.0.8.6 /usr/lib64/libsqlite3.so /usr/local/sqlite3/lib/libsqlite3.so.0.8.6 /usr/local/sqlite3/lib/libsqlite3.so.0 /usr/local/sqlite3/lib/libsqlite3.so
再重新编译安装python3
-
启动项目
python3 manage.py runserver
搭建虚拟环境(Ubuntu)
-
安装 虚拟环境
sudo pip3 install virtualenv sudo pip3 install virtualenvwrapper
-
创建目录用来存放虚拟环境
mkdir ~/.virtualenvs
-
打开 ~/.bashrc 文件,并添加如下:
export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
-
运行
source ~/.bashrc
-
在python3中,创建虚拟环境
mkvirtualenv -p python3 py3_django
-
使用虚拟环境的命令
workon 参考当前所有虚拟环境 workon 虚拟环境名称 使用虚拟环境 deactivate 退出虚拟环境 rmvirtualenv 虚拟环境名称 删除虚拟环境 -
在虚拟环境安装包
pip install django
查看虚拟环境中安装的包pip list
搭建虚拟环境(window 10)
创建虚拟环境 | mkvirtualenv |
退出虚拟环境 | deactivate.bat |
使用虚拟环境 | workon 虚拟环境名 |