python入门

开发环境安装

  1. 下载Python 可以选择安装版 Windows x86 executable installer
  2. 下载pycharm 我用的是pycharm professional 2018.2
  3. 打开python工程,自动识别,选择安装依赖。
  4. Run/Debug Configurations 配置启动入口文件

PIP更新

pip install –upgrade pip # python2.x linux
pip3 install –upgrade pip # python3.x linux
python -m pip install -U pip # python2.x windows
python -m pip3 install -U pip # python3.x windows

\Lib\site-packages 依赖的位置

打开pycharm→File→Setting→Project→Project Interpreter→pip

pycharm→File→Setting→Project→Project Interpreter→点击加号→ManageRepositories设置仓库地址:清华大学镜像 https://pypi.tuna.tsinghua.edu.cn/simple

依赖安装

新环境可以通过复制site-packages里面的包进行依赖包的拷贝。注意pyvenv.cfg等配置文件内容不需要替换

遇到问题

from werkzeug import secure_filename,FileStorage 报错cannot import name ‘FileStorage’

出现cannot import secure_filename把from werkzeug 改为from werkzeug.utils

改完出现cannot import name ‘FileStorage’ 分两行写:
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage

基本语法

模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# support.py 模块:
def print_func( param ):
print "Hello : ", param
return

# 导入模块
import support

# 现在可以调用模块里包含的函数了
support.print_func("Run")

# from…import 语句
from modname import function1[, function2[, ... functionN]]

# python包
__init__.py 用于标识当前文件夹是一个包,里面可以有非常丰富的内容



安装第三方包

pip install -U socketIO-client

pycharm中:pycharm→File→Setting→Project→Project Interpreter→点击加号→搜索→Install Package

类、函数

函数(def)、 类(class)

1
2
# 入口
if __name__ == '__main__':

其他

call

如果在类中实现了 __call__ 方法,那么实例对象也将成为一个可调用对象

flask web框架

manage.py通过Run/Debug Configurations → Configuration Additional options:添加--host 0.0.0.0 --port 8088指定其他服务器可以访问以及端口

移植问题

--disable-ipv6 导致不支持IPV6,可能需要重新编译增加模块。可以通过family=socket.AF_INET6来限制监听v4还是v6。

参考