用import从外部模块获取函数并且为自己程序所用
编写自己的模块
模块是程序
任何Python程序都可以作为模块导入
名字很重要!
syntaxError invalid syntax;
我们只是告诉解释器 除了默认的目录中寻找 还需要目录中寻找模块
导入自己的模块
模块名是程序的 文件名
.pyc为扩展名文件 平台无关经过处理 python能够更加有效处理的文件
导入模块并不意味着导入时执行某些操作 导入模块主要用于定义 变量 函数 类
此外 因为只是定义这些东西 导入模块一次和多次效果一样
import-only-once
循环终止
第二次导入模块时候什么都不会发生
模块
函数定义,包含函数简单模块
执行模块下的 函数
模块.函数()
moduleName.Functation()
NameError:name is not defined;
通过同样的方法来使用任何在模块
模块中增加测试代码
模块用来定义函数 类和其他内容
模块中增加一些检查模块本身是否正常工作的测试代码
带有问题测试代码简单模块
模块本身是作为程序运行还是导入到其他程序
模块本身 作为程序运行还是导入其他程序
__name__变量
模块本身作为程序运行还是导入到其他程序
__name__
主程序 解释器的交互式提示符在内
变量__name__值 是__main__
导入模块 __name__值被设定为模块名字
让测试代码更加有用
使用条件测试代码模块
改变sys.path 其中包含了(字符串组成的)一个目录列表,解析器在该列表查找模块
一开始sys.path本身就应该包含正确的目录(你的模块的目录)
2种 方法做到
将模块放在合适的位置
另外是告诉解释器到哪里去找需要的模块
找出解释器从哪里寻找模块
然后将自己 的文件放置在那里即可;
告诉解释器去那里寻找模块
存放自己的模块到 python目录
那些称为搜索路径的目录的列表可以在sys模块中path变量中找到;
sys模块的path变量中 找到
命名模块
包含模块代码文件的名字和模块名一样
组织模块--包
分组为包package
包基本上是另一类模块
包基本就是另外模块 包能够包含其他模块
当模块存储在文件中 包就是模块存储所在的目录
为了让Python将其作为包对待,必须包含一个命名为__init__py文件模块
将它
名为constrants包
文件constrants/__init__.py包括语句PI=3.14;
import constrants
print(constrants.PI)
建立drawing包,包括shapes和colors模块,需要创建所示文件及目录
/python/
/python/drawing/ 包目录drawing包
/python/drawing/__init__.py 包代码drawing模块
/python/drawing/colors.py colors模块
/python/drawing/shapes.py
包 就是目录,包 只是为了更好的组织模块;
import drawing #(1)Imports the drawing package
import drawing.colors #(2)Imports the colorsmodule
from drawing import shapes
查看模块包含的内容可以使用dir函数,它会将对象的所有特性列出 模块的所有函数,类 变量
看到一长串名字
名字以下划线开始 暗示 约定俗成
并不是为在模块外部使用而准备
列表推倒
并不是为在模块外部使用准备
__all__变量
__all__ = ["Error","copy","deepcopy"]定义公有接口(public interface)
from copy import *
显示的实现
编写模块使用__all__
模块可能会有一大堆其他程序不需要不想要的变量 函数类
__all__ 客气的将他们过滤;
import *语句默认将会输出模块中所有不以下划线开头全局名称
那个模块下的函数
help on function copy in module copy;
copy(X)
shallow copy
文档字符串写在函数开头并且简述2函数功能字符串
字符串可以通过函数的__doc__特性引用
函数 模块 类;
return a list containing an arithmetic progression of integers
the end point is omitted
exactly
valid indices
elements
描述Python丰富的标准库
batteries included
batteries
batteries battieres
充电电池
激发对模块探究兴趣热情
sys模块让你能够访问与Python解释器联系紧密的变量函数
TypeError list object is not callable;
object is not callable;
映射模块名字到载入模块的字典
sys.path查找模块所在目录目录名列表
变量sys.argv包括传递到python解释器参数 包括脚本名称
函数sys.exit课可以退出当前程序
sys.apth模块变量 字符串列表 每个字符串都是一个目录名称
import语句执行 解释器会从这些目录中查找模块
sys.platform模块变量 字符串 是解释器正在其上运行的平台
可能是标识操作系统的名字
模块名文件流对象
sys.platform
sys.stdin sys.stdout
sys.stderr;
标识标准unix概念中标准输入标准输出 标准错误
Python利用sys.stdin获取输入
获取输入
input raw_input
反序打印参数
command-line argument
sys.ardgv
sys.argv[0]
反序打印参数
install and configure controller node
prerequisites
create database,service credential,API endpoints;
创建服务凭证
service credential credentials credentials
complete these steps;
credentials
credentials;
configure networking options
you can deploy the networking service using one of two architectures represented by options
Option 1 deploy the simplest possible architecture that only supports attaching instances to public(provider) network
no self-service networks,routers orfloating ip addressing only the admin or other privileged user can manage
option2 augments option1 with layer-3 services that support attaching instances to self-service(private)
支持将实例添加到自助服务网络 私有网络
支持将实例添加到自助服务网络
support attaching instances to self-service(private) network
the demo or other unprivileged user can manage self-service network including
routers that provide connectivity between self-service and provider networks
additionally,floating ip address provide connectivity to instances
choose one of the following networking options to configure services specific to it,
server compont
networking server compont configuration includes the database,authentication mechanism,message queue,topology change nofifications,and plug-in;
数据库
includes the database authentication mechanism message queue topology change notifications and plug-ins
vary by distribution
you might need to add these sections and options rather than modifying existing sections and
ellipsis
snippets
snippets 片段 标示indicates indicates potential
default section enable the moduler layer2 ML2 plugin and disable additional plug-ins
default oslo_messaging_rabbit sections configure rabbitMQ message queue access
in the [DEFAULT] and [nova] sections,configure networking to notify compute of network topology changes;
modular layer 2(ML2) plug-in
ML2 plug-in uses the linux bridge mechanism to build layer-2 virtutal networking infrastructure for instances;
the ML2 plug-in uses the linux bridge mechanism to build layer-2 bridging and switching virtual networking infrastructure for instances;
edit file and complete the following actions
[ml2]section,enable flat and vlan networks;
enable flat and vlan networks;
in the [ml2] section,disable project(private) networks
disable project networks
enable the linux bridge mechanism
warning
after you configure the ML2 plug-in removing values in the type_drivers options can lead to database inconsistency;
ml2 section,enable the port security extension driver;
ml2_type_flat configure the public flat provider network;
in the [securitygrooup] section,enable ipset to increase efficiency of security group rules;
including vxlan tunnels for private netwoorks and handles security groups
map the public virtual networkk to the public physcial network interfaces;
cinfigure the DHCP agent
providers DHCP services for virtual networks
configure the linux bridge interface driver dnsmasq dhcp driver and enable isolated metadata so instances on public networks can access metadata over the network;
the metadata agent providers configutions information such as credentials to instances
configure access parameters,enable the metadata proxy and configure the secret;
the openstack dashboard also known as horizon is a web interface that enables cloud administrators and users to manage various openstack resources and services
web-based interactions with the openstack compute cloud controller through he
horizon ennables you to customize the brand of the dashboard
this section describes how to install and configure the dashboard on the controller node
relies on functional core services including identiified
user as the default rile for users tat you create via the dashboard
if you chose networking option 1,disable support for layer-3 networking services
launch an instance
create virtual networks
generatea key pair,add security group rules
and one instance that uses each network
networking option 1 includes one public virtual network and one instance that uses it networking option 2 includes one public virtual network one private virtual network,and one instance that uses each network
an instance uses a public provider virtual network that connects to the physical network infrastructure via layer-2(bridging/switching) this network includes a DHCP server that providers ip addresses to instances
the admin or other privileged user create this network because it connects directly to the physcial network infrastructure
Note
the following instructions and disgrams use example ip address ranges,you must adjust them for your particular environment;
Networking option 1:Provider Networks overview
the --shared options allows all projects to use the virtual network
the --provider:physical_network public and --provider:network_type flat options connect the flat virtual network to the flat(native/untagged)public physicla network on the eth1 interface on the host using information from the following files
ml2_conf.ini
[ml2_type_flat]
flat_network = public
linuxbridge_agent.ini
[linux_bridge]
physical_interface_mappings = public:eth1
create a subnet on the network
neutron subnet-create public PUBLIC_NETWORK_CIDR --name public --allocation-pool start=START_IP_ADDRESS,end=END_IP_ADDRESS --dns-nameserver DNS_RESOLVER --gateway PUBLIC_NETWORK_GATEWAY
after creating the appropriate networks for your environment you can continue the environment to launch an instance
generate a key pair
most cloud image support public key authentication rather conventional password authentication before launching an instance,you must add a public key to the compute service
add security group rules
by default the default security group applies to all instances and includes firewall rules that deny remote access to instances,for linux images such as cirros,we recommand allowing at least ICMP ping and secure shell ssh
lanuch an instances
if you chose networking option 1,you can only launch an instance on the public,if you chose networking option 2
you can launch on the public network and
a flavor specifies a virtual resource allocation profile which includes processor,memory,and storage
separate;
os.urandom(n) 返回n字节的加密强随机数据
os.lineseq 行分隔符
os.pathseq 分割路径的分隔符
os.environ
os.system(command)
os.environ映射包含本章环境变量
访问系统变量
TypeError _Environ object is not callable;
映射可以更改系统环境变量
os.system函数用于运行外部程序 execv
将控制权交给被执行程序
标准库文档
popem
os.sep 模块变量用于路径中的分隔符
\\
unix Mac OS x 中命令行版本的Python中的标准分隔符是 '/'
/etc/ruiy.txt
\\ 反斜杠
/斜杠
\\ Python针对单个反斜杠的语法
;
;
文本文件字符串分隔符\r\n
\n
\r
两者结合
Mac OS \r
unix/linux \n
\r\n
行尾分隔符
os.linesep
os.pathsep
;行分隔符
os.sep 路径分隔符
组织路径使用os.pathsep
; : ::
路径分隔符\\
/
:
a:v:b
c:\\a\\f
/etc/
windows linux Mac OS
os.sep(\\,/,:路径名中的分隔符) \\ / :
os.linesep \r\n \n \r
os.pathsep 多个路径之间的分割 ; : ::
not implemented error
system命令可以执行外部程序
通过命令行执行程序的环境很有用
对图形用户启动程序很有用
original
primitive
firsthand;
os.system('/usr/bin/firefox')
os.system(r'c:\"Program Files"\"Mozilla Firefox"\firefox.exe')
将Program Files和Mozilla Firefox放入引号 不然Dos 它负责处理这个命令 就会停下来
对于PythonPATH设定的目录,这点也同样重要
os.startfile;
包括空格也没有问题
不用像os.system将Program Files 放在引号中
在windows由os.system(或者os.startfile)启动了外部程序之后,python程序仍然继续运行
unix 程序终止,等待等等 os.system完成
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2015-02-03 EOF 空格问题
2015-02-03 dataGuard client 自动切换
2015-02-03 dataGuard主备库角色切换
2015-02-03 oracle字符集查看修改
2015-02-03 oracle查看所有表及字段
2015-02-03 oracle重新启动步骤
2015-02-03 oracle job