python学习1-概述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/you_jinpeng/article/details/90246385

这里写自定义目录标题

一 ,pip 的使用
1.pip list 列出当前已经安装的所有模块
2.pip install XXX[version]
例如 pip install test
1.0.1
3.pip install --upgrade xxx
例如 pip install --upgrade test
4.pip install -r requirements.txt 安装requirements.txt文件中指定的扩展库
5.pip uninstall XXX[version]
例如 pip uninstall test
1.0.1

二,导入模块的方式
1.import 模块名 [as 别名]
例如:>>>import math
>>>math.sin(0.5)
0.4794524845648

2.from 模块名 import 对象名 [as 别名]
例如:>>>from math import sin
>>>sin(0.5)
0.4794524845648

3.from 模块名 import *
例如:>>>from math import *
>>>sin(0.5)
0.4794524845648
>>>gcd (36,18)#最大公约数
18
>>>log2(8)
3.0
三,使用append()方法添加自定义文件夹以扩展库函数的包含路径
>>>import sys
>>>sys.path.append(‘testZip.zip’)

四,一些属性
1._ name _
直接运行程序时候编译为_ _main _ _,调用时候编译为模块名称
例子:假设程序hello.py中的代码如下:
def main():
if _ name ==’ _main _ ’ :
print(‘1’)
elif _ name ==‘hello’:
print(‘2’)
在该例子中。当直接运行该程序时候输出1
在其它模块调用该函数时候输出2
注:在main()也只是一个普通函数
2.
init all _
如果在
init _.py中有如下代码:
_ all =[‘ui2’,'CNN_cifar’等等]
那么就可以使用
all _导入变量指定的ui2,CNN_cifar等等对象:
from bishelast.ui2 import *

五,伪编译 反伪编译
伪编译:py——>pyc,反伪编译相反
1.伪编译
import py_compile
py_compile.compile(‘test.py’)
或者
import compileall
compileall.compile_file(‘test.py’)
生成文件与_ pycache _
2.反伪编译
import uncompyle6
uncompyle6. uncompyle_file(’_ pycache \test.cpyrhon-35.pyc’,
open(’
pycache _\test.py’,‘w’))
3.伪编译方法二:转化为二进制可执行文件(更好,无法反伪编译)
(1)py2exe(仅适用于windows)
例子:假设有源文件test.py
编写setup.py,内容如下:
import distutils
import py2exe
distutils.core.setup(console=[‘test.py’])#注意:对于GUI,则把console改为windows
然后在命令提示符执行:python setup.py py2exe
(2)对于简单的程序,在命令提示符执行:build_exe test.py就可以生成exe可执行文件了
(3)pyinstaller
在命令提示符执行:
pyinstaller -F -w test.pyw(或者python pyinstaller-script.py -F -w test.pyw)
(w是否要加?)

猜你喜欢

转载自blog.csdn.net/you_jinpeng/article/details/90246385