Python 官方文档:入门教程 => 点击学习
鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper。 virtualenvwrapper提供了一系列命令使得和虚拟环境工作变得便利。它把你所有的虚拟环境都放在一个地方。python 开发的项
鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper。 virtualenvwrapper提供了一系列命令使得和虚拟环境工作变得便利。它把你所有的虚拟环境都放在一个地方。
python 开发的项目,放到线上服务器运行。那么服务器系统,必然是linux系统。因为Linux的性能优于windows系统。
由于线上项目使用的Django项目版本不同。早几年前的的项目还是1.x版本,现在新写的项目,是2.x版本。那么对于一台服务器,只能有一个Python版本。也就对应着,只有一个DjanGo版本。
这个时候,就需要使用virtualenvwrapper了。
确保virtualenv已安装
[root@localhost ~]# pip3 install virtualenv
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting virtualenv
如果出现以上信息,说明要安装openssl
yum install -y openssl openssl-devel
yum clean all
再次执行命令pip3 install virtualenv,发现还是同样的问题。
查阅资料发现,在./configure过程中,如果没有加上–with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,刚好pip3过程需要ssl模块,而由于没有指定,所以该功能不可用。
解决办法是重新对python3.6进行编译安装,用一下过程来实现编译安装:
cd /usr/src/Python-3.6.5/
./configure --with-ssl --prefix=/usr/local/python3
make -j4 && make install
cd /root/
再次执行安装命令,就可以了
pip3 install virtualenv
提示以下信息:
You are using pip version 9.0.3, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
升级pip
[root@localhost ~]# pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 593kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-10.0.1
安装virtualenvwrapper
pip3 install virtualenvwrapper
linux还需要安装一个包,否则找不到virtualenvwrapper.sh文件
yum install python-virtualenv -y
首选找到对应的virtualenvwrapper.sh文件的路径:
[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/local/python3/bin/virtualenvwrapper.sh
创建虚拟环境目录
mkdir /virtualenvs
找到路径之后在/etc/profile中进行配置:
vim /etc/profile
export WORKON_HOME=/virtualenvs
source /usr/local/python3/bin/virtualenvwrapper.sh
第一行:virtualenvwrapper存放虚拟环境目录
第二行:virtrualenvwrapper会安装到python的bin目录下,所以该路径是python安装目录下
读入配置文件,立即生效
[root@localhost virtualenvs]# source /etc/profile
virtualenvwrapper.user_scripts creating /virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /virtualenvs/initialize
virtualenvwrapper.user_scripts creating /virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /virtualenvs/pReactivate
virtualenvwrapper.user_scripts creating /virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /virtualenvs/get_env_details
如果运行报错,如下:
/usr/bin/python: No module named virtualenvwrapper
请确保python版本为3.6。如何将系统的python版本替换为3.6,请参考文章
Http://www.py3study.com/Article/details/id/320.html
virtualenvwrapper基本使用
1.创建虚拟环境 mkvirtualenv
cd /virtualenvs
mkvirtualenv venv1
这样会在WORKON_HOME变量指定的目录下新建名为venv的虚拟环境。
若想指定python版本,可通过"--python"指定python解释器
mkvirtualenv --python=/usr/local/python3/bin/python3.6 venv1
上面这一步不需要执行,python默认已经更改为3.6了
2.基本命令
查看当前的虚拟环境目录
(venv1) [root@localhost virtualenvs]# workon
venv1
(venv1) [root@localhost virtualenvs]#
切换到虚拟环境
语法:workon 虚拟目录名
退出虚拟环境
(venv1) [root@localhost virtualenvs]# deactivate
[root@localhost virtualenvs]#
删除虚拟环境
[root@localhost virtualenvs]# rmvirtualenv venv1
Removing venv1...
[root@localhost virtualenvs]#
下面开始创建2个虚拟目录,分别来安装2个django版本。
配置pip更新源,因为国外的速度太慢了。改为国内豆瓣源
[root@localhost ~]# mkdir ~/.pip/
[root@localhost ~]# vim ~/.pip/pip.conf
添加内容如下:
[global]
trusted-host = pypi.douban.com
index-url = http://pypi.douban.com/simple
创建2个虚拟目录,直接在根目录执行也可以
mkvirtualenv venv1
安装django,指定版本为1.8
(venv1) [root@localhost ~]# pip3 install django==1.8
Looking in indexes: http://pypi.douban.com/simple
Collecting django==1.8
Downloading http://pypi.doubaNIO.com/packages/4e/1c/17a429cfb79c1814d1ec31939fc5cf4a8ac68fe934279e095fb6160123a9/Django-1.8-py2.py3-none-any.whl (6.2MB)
100% |████████████████████████████████| 6.2MB 11.0MB/s
Installing collected packages: django
Successfully installed django-1.8
(venv1) [root@localhost ~]#
创建项目
进入虚拟目录
cd /virtualenvs/venv1/
创建站点mysite
django-admin startproject mysite
创建应用blog
cd mysite/
python manage.py startapp blog
提示报错:
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'
安装sqlite所有组件
yum install sqlite*
再次对Python3.6进行重新编译
cd /usr/src/Python-3.6.5/
./configure --with-ssl --prefix=/usr/local/python3
make -j4 && make install
cd /root/
切换到虚拟环境venv1,重启创建blog。并启动项目!
[root@localhost ~]# workon venv1
(venv1) [root@localhost ~]# cd /virtualenvs/venv1/mysite/
(venv1) [root@localhost mysite]# python manage.py startapp blog
(venv1) [root@localhost mysite]# python manage.py runserver
PerfORMing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
June 23, 2018 - 11:52:48
Django version 1.8, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
访问页面,哦!不对。这是Linux,不能直接用127.0.0.1
重新启动,监听本机的所有IP地址。
(venv1) [root@localhost mysite]# python manage.py runserver 0.0.0.0
CommandError: "0.0.0.0" is not a valid port number or address:port pair.
(venv1) [root@localhost mysite]# python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
June 23, 2018 - 11:55:00
Django version 1.8, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[23/Jun/2018 11:56:04]"GET / HTTP/1.1" 200 1767
使用内网IP加端口访问即可!
使用xhell复制一个ssh窗口,创建虚拟目录venv2
mkvirtualenv venv2
#安装最新版diango
pip3 install django
进入虚拟目录
cd /virtualenvs/venv2/
创建站点mysite
django-admin startproject mysite
创建应用blog
cd mysite/
python manage.py startapp blog
启动项目
python manage.py runserver 0.0.0.0:8001
使用内网IP加端口,注意端口是8001
提示IP不允许
进入mysite目录,修改settings.py
(venv2) [root@localhost mysite]# cd mysite/
(venv2) [root@localhost mysite]# vim settings.py
增加*,表示允许所有IP。注意,要有一个引号
ALLOWED_HOSTS = ['*']
再次启动应用,访问url
(venv2) [root@localhost mysite]# cd ../
(venv2) [root@localhost mysite]# python manage.py runserver 0.0.0.0:8001
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 23, 2018 - 13:13:51
Django version 2.0.6, using settings 'mysite.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CONTROL-C.
网页效果如下:
--结束END--
本文标题: Centos7 安装python虚拟环境+Django
本文链接: https://lsjlt.com/news/179142.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0