東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

python3学习之实战篇

2017年5月23日星期二



我决定把python3.4升级到python3.5
好像很麻烦,还是算了。。。。

2016年07月11日11:07:29
我觉得还可以再尝试一下

0,升级python3.4到python3.5


1)下载
https://www.python.org/downloads/
下载得到Python-3.5.2.tar.xz

2)编译安装
然后解压,找到README,执行
  1. senrsl@senrsl-T540p:~/python/Python-3.5.2$ ./configure
  2. senrsl@senrsl-T540p:~/python/Python-3.5.2$ make
  3. senrsl@senrsl-T540p:~/python/Python-3.5.2$ make test
  4. senrsl@senrsl-T540p:~/python/Python-3.5.2$ sudo make install
  5. 。。。。
  6. Collecting setuptools
  7. Collecting pip
  8. Installing collected packages: setuptools, pip
  9. Successfully installed pip-8.1.1 setuptools-20.10.1
  10. senrsl@senrsl-T540p:~/python/Python-3.5.2$ python --version
  11. Python 2.7.6
  12. senrsl@senrsl-T540p:~/python/Python-3.5.2$ python3 --version
  13. Python 3.5.2
  14. senrsl@senrsl-T540p:~/python/Python-3.5.2$
这就升级到3.5了


3)验证语法
升级
  1. senrsl@senrsl-T540p:~/test/python3$ cp asyncio/test_asyncio_demo.py asyncio/test_async.py
  2. senrsl@senrsl-T540p:~/test/python3$ vi asyncio/test_async.py
  3. senrsl@senrsl-T540p:~/test/python3$ python3 asyncio/test_async.py
  4. hello,world!!!!!
  5. hello again!
  6. senrsl@senrsl-T540p:~/test/python3$ cat asyncio/test_async.py
  7. #!/usr/bin/env python3
  8. # -*- coding:utf-8 -*-
  9. import asyncio;
  10. async def hello():
  11.     print("hello,world!!!!!");
  12.     r = await asyncio.sleep(1);
  13.     print("hello again!");
  14. loop = asyncio.get_event_loop();
  15. loop.run_until_complete(hello());
  16. loop.close();
  17. senrsl@senrsl-T540p:~/test/python3$
不错

1,准备

1)环境准备
升级了python3.5.2之后,就提示升级pip3,然后安装异步框架aiohttp,前端模板引擎jinja2,mysql异步驱动 aiomysql.
  1. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ sudo pip3 install aiohttp
  2. [sudo] password for senrsl:
  3. The directory '/home/senrsl/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  4. The directory '/home/senrsl/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  5. Collecting aiohttp
  6.   Downloading aiohttp-0.21.6.tar.gz (528kB)
  7.     100% |████████████████████████████████| 532kB 1.3MB/s
  8. Collecting chardet (from aiohttp)
  9.   Downloading chardet-2.3.0.tar.gz (164kB)
  10.     100% |████████████████████████████████| 174kB 222kB/s
  11. Installing collected packages: chardet, aiohttp
  12.   Running setup.py install for chardet ... done
  13.   Running setup.py install for aiohttp ... done
  14. Successfully installed aiohttp-0.21.6 chardet-2.3.0
  15. You are using pip version 8.1.1, however version 8.1.2 is available.
  16. You should consider upgrading via the 'pip install --upgrade pip' command.
  17. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ sudo pip3 install --upgrade pip
  18. The directory '/home/senrsl/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  19. The directory '/home/senrsl/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  20. Collecting pip
  21.   Downloading pip-8.1.2-py2.py3-none-any.whl (1.2MB)
  22.     100% |████████████████████████████████| 1.2MB 917kB/s
  23. Installing collected packages: pip
  24.   Found existing installation: pip 8.1.1
  25.     Uninstalling pip-8.1.1:
  26.       Successfully uninstalled pip-8.1.1
  27. Successfully installed pip-8.1.2
  28. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$
  29. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ sudo pip3 install jinja2
  30. The directory '/home/senrsl/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  31. The directory '/home/senrsl/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  32. Collecting jinja2
  33.   Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB)
  34.     100% |████████████████████████████████| 266kB 1.2MB/s
  35. Collecting MarkupSafe (from jinja2)
  36.   Downloading MarkupSafe-0.23.tar.gz
  37. Installing collected packages: MarkupSafe, jinja2
  38.   Running setup.py install for MarkupSafe ... done
  39. Successfully installed MarkupSafe-0.23 jinja2-2.8
  40. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$
  41. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ sudo pip3 install aiomysql
  42. The directory '/home/senrsl/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  43. The directory '/home/senrsl/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
  44. Collecting aiomysql
  45.   Downloading aiomysql-0.0.7.tar.gz (52kB)
  46.     100% |████████████████████████████████| 61kB 365kB/s
  47. Collecting PyMySQL<=0.6.7 (from aiomysql)
  48.   Downloading PyMySQL-0.6.7-py2.py3-none-any.whl (69kB)
  49.     100% |████████████████████████████████| 71kB 851kB/s
  50. Installing collected packages: PyMySQL, aiomysql
  51.   Running setup.py install for aiomysql ... done
  52. Successfully installed PyMySQL-0.6.7 aiomysql-0.0.7
  53. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$
不要在意那些细节

2)项目结构
  1. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ tree
  2. .  根
  3. ├── backup  备份目录
  4. ├── conf    配置文件
  5. ├── dist    打包目录
  6. ├── ios    IOS app工程
  7. ├── LICENSE 
  8. └── www    web目录
  9.     ├── static    静态文件
  10.     └── templates    模板文件
  11. 8 directories, 0 files
  12. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$

2,Web App 开发


1)编写web app骨架
一个协程的异步http
  1. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$ cat www/app.py
  2. #!/usr/bin/env python3
  3. # -*- coding:utf-8 -*-
  4. import logging;logging.basicConfig(level=logging.INFO);
  5. import asyncio,os,json,time;
  6. from datetime import datetime;
  7. from aiohttp import web;
  8. def index(request):
  9.     return web.Response(body=b"<h1>Awesome</h1>");
  10. @asyncio.coroutine
  11. def init(loop):
  12.     app = web.Application(loop=loop);
  13.     app.router.add_route("GET","/",index);
  14.     srv = yield from loop.create_server(app.make_handler(),"127.0.0.1",9999);
  15.     logging.info("server started at http://127.0.0.1:9999....");
  16.     return srv;
  17. loop = asyncio.get_event_loop();
  18. loop.run_until_complete(init(loop));
  19. loop.run_forever();
  20. senrsl@senrsl-T540p:~/test/python3/awesome-python3-webapp$


1-3)使用eclipse
引入eclipse编辑器
下载,安装pydev插件
建个python项目发现多了.project跟.pydevproject俩配置文件
引入

2)编写ORM




2017年05月23日10:36:19
接近一年没动,多半是废了。。。。





--
senRsl
2016年07月09日13:47:50

没有评论 :

发表评论