Linux平台Python运行环境配置

1.软件包管理工具:pip

[xiaokang@localhost ~]$ sudo yum  install python2-pip

     查看pip版本

[xiaokang@localhost ~]$ pip -V  (大v)
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

 

    将pip源换成豆瓣源

[xiaokang@localhost ~]$ mkdir ~/.pip
[xiaokang@localhost ~]$ touch ~/.pip/pip.conf
[xiaokang@localhost ~]$ vim ~/.pip/pip.conf 
#添加以下内容
[global]
timeout = 60
index-url = https://pypi.doubanio.com/simple

 

2.虚拟环境:virtualenv (设置多个环境,可以同时存在几个Python版本环境)

 

[xiaokang@localhost ~]$ sudo pip install virtualenv

      创建xiaokang虚拟环境

[xiaokang@localhost ~]$ virtualenv xiaokang
  No LICENSE.txt / LICENSE found in source
New python executable in /home/xiaokang/xiaokang/bin/python2
Also creating executable in /home/xiaokang/xiaokang/bin/python
Installing setuptools, pip, wheel...
done.

 

       进入和退出到xiaokang虚拟环境中

[xiaokang@localhost ~]$ source xiaokang/bin/activate
(xiaokang) [xiaokang@localhost ~]$ python
Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
(xiaokang) [xiaokang@localhost ~]$ which python
~/xiaokang/bin/python
[xiaokang@localhost ~]$ source xiaokang/bin/activate   # 进入到xiaokang虚拟环境
(xiaokang) [xiaokang@localhost ~]$ deactivate   # 退出虚拟环境

 

    安装requests依赖包

 

(xiaokang2) [xiaokang@localhost ~]$ pip install requests

    requests使用指南 https://2.python-requests.org//zh_CN/latest/user/quickstart.html

 

   安装flask框架

(xiaokang) [xiaokang@localhost ~]$ pip  install  -i  https://pypi.doubanio.com/simple/  --trusted-host pypi.doubanio.com  flask

 

   配置flask并启动

 

(xiaokang) [xiaokang@localhost ~]$ mkdir python
(xiaokang) [xiaokang@localhost ~]$ cd python/
(xiaokang) [xiaokang@localhost python]$ touch index.py
(xiaokang) [xiaokang@localhost python]$ vim index.py 
# 添加以下内容
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello World!'
if __name__ == '__main__':
    app.run(host='0.0.0.0')

# 启动flask
(xiaokang) [xiaokang@localhost python]$ python index.py 

以上就是Python环境搭建和项目部署实际过程,可以在浏览器访问http://192.168.0.1:5000,页面出现 Hello World

 

 

你可能感兴趣的:(Linux系统)