《Python入门》Linux 下 Python Web开发环境搭建笔记 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 网站建设 >

《Python入门》Linux 下 Python Web开发环境搭建笔记

发表时间:2016-4-26

发布人:葵宇科技

浏览次数:50

之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境。

我使用的系统是:ubuntu 14.04 server,CentOS 参考我的最新记录:CentOS release 6.10 下 Python 3.7.5 安装记录。

关于Python的版本

进入Python的网站,鼠标移到导航条上的下载,我们会发现提供两下主版本的下载链接!

这两个之间存在什么差别呢?

个人理解,2.7.x的版本为更加稳定的版本,而3.x的版本则是比较前卫的版本,包含了很多新功能新特性之类的;

但如果想要用Python开发一个新项目,那么该如何选择Python版本呢?大部分Python库都同时支持Python 2.7.x和3.x版本的,所以不论选择哪个版本都是可以的。

但有一些需要注意的区别,参考:Python 2.7.x 和 3.x 版本的重要区别小结

下载Python

由于Bluemix中如果未指定版本,缺省情况下会选择 V2.7.10,所以我决定下载安装 V2.7.10。

我的操作系统是ubuntu 14.04 server,Linux系统通常都是通过源码进行安装;

可以直接使用以下命令下载:

wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xz

点击下图进入Python下载页面,找到下载地址;

选择你想使用的版本:

XZ格式的压缩率通常是最高的,所以我这里选择XZ格式的包;

安装Python

解压刚刚下载的安装包

# xz -d Python-2.7.11.tar.xz
# tar -xvf Python-2.7.11.tar

进入文件目录:

# cd Python-2.7.11

配置

# ./configure

安装

# make

这里只需要执行 make 就可以了, 不需要执行 makeinstall。

注意,系统中可能还没有安装 make,会出现如下提示:

# make
The program 'make' is currently not installed. You can install it by typing:
apt-get install make

然后我们可以根据提示,执行以下命令安装:

# apt-get install make

输出如下:

# apt-get install make
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  make-doc
The following NEW packages will be installed:
  make
0 upgraded, 1 newly installed, 0 to remove and 48 not upgraded.
Need to get 119 kB of archives.
After this operation, 328 kB of additional disk space will be used.
Get:1 http://hk.archive.ubuntu.com/ubuntu/ trusty/main make amd64 3.81-8.2ubuntu3 [119 kB]
Fetched 119 kB in 7s (16.0 kB/s)
Selecting previously unselected package make.
(Reading database ... 80371 files and directories currently installed.)
Preparing to unpack .../make_3.81-8.2ubuntu3_amd64.deb ...
Unpacking make (3.81-8.2ubuntu3) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up make (3.81-8.2ubuntu3) ...

安装成功之后再执行 make 命令,这个过程可能有点慢,需要等一会儿。

以下为最后部分输出:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _curses            _curses_panel   
_sqlite3           _ssl               _tkinter        
bsddb185           bz2                dbm             
dl                 gdbm               imageop         
readline           sunaudiodev        zlib            
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
creating build/scripts-2.7
copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /home/aven/Python-2.7.11/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /home/aven/Python-2.7.11/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py

到这里就安装完成了,输入“Python”,然后回车看看吧!

如何退出 Python 命令行? Ctrl + Z 就可以了。

关于Pip

pip是一个安装和管理Python包的工具,是easy_install的一个替换品。

看到网上一些文章介绍Pip还需要单独安装,已经过时了,经过上面的步骤,Pip已经被安装好了;

关于Python IDE

什么是IDE?
IDE= 集成开发环境= 把开发相关的各种环境(和工具)都集成到一起

Python IDE= Python的集成开发环境= 把和Python开发相关的各种工具

有几种可以选择:

  • PyDev
  • Komodo Edit
  • PyCharm
  • Atom with Python plugin

Hello World

Python要求严格的缩进:

[python] view plain copy
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. print 'hello world'
  4. for i in range(1,5):
  5. for j in range(1,5):
  6. for k in range(1,5):
  7. if( i != k ) and (i != j) and (j != k):
  8. print 'hello world', i,j,k

保存文件,执行查看结果:

小结

到这里Python Web的开发环境就搭建完了,非常的简单!

有的小伙伴可能怒了,我还没看到Web的影子呢!

哈哈,我也是刚刚发现,Python和Go语言一样,Web服务器可以自己写;

下一篇:《Python入门》第一个Python Web程序——简单的Web服务器

相关案例查看更多