博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
导入自定义模块model
阅读量:6969 次
发布时间:2019-06-27

本文共 1013 字,大约阅读时间需要 3 分钟。

编写m2.py,脚本内容如下:

#!/usr/bin/python# -*- coding: utf-8 -*-'its a module test'__author__ = 'mm'import sys#定义函数ft()def ft(x):  x=int(x)  sum=x*3  print sum#ft(sys.argv[1])

直接运行脚本 ./m2.py 3,返回值 9

在python命令行下运行 import m2,导入m2.py(此为自定义的模块)

运行 dir(m2) ,返回如下:

['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'ft', 'sys']

运行 m2.ft(33),调用m2模块中的ft()函数,返回值 99

 

 ==========导入目录============

 

目录scripts下有文件cpu.py

1.目录scripts下新建空文件 __init__.py (python3下可以从目录下导入)

2.导入:
from scripts import cpu
from scripts import *

可以从py文件中导入Class,Function等直接使用

 

如果目录scripts在其他目录下,未与当前py文件位于同一目录,则可使用多级os.path.dirname将其目录或父目录加入到当前环境变量

print os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #返回脚本所在目录的父目录,如C:\Django\workplace\sf

sys.path.append('basedir') #只是当前生效。

查看当前环境变量: print sys.path

然后再导入即可。

 

aa2.py中包含有一个字典d1和一个list l:

d1 = {'k1':1,'k2':222}l = [1,2,3,8,9]

比如将aa2.py导入后,可以直接使用其字典d1和l,如下:

from aa import aa2print aa2.d1['k2']print aa2.l[3]返回:2228

 

转载于:https://www.cnblogs.com/dreamer-fish/p/5123632.html

你可能感兴趣的文章
HBase2.0中的Benchmark工具 — PerformanceEvaluation
查看>>
基于 Docker 打造前端持续集成开发环境
查看>>
[case1]记一次spring schedule异常
查看>>
五分钟了解微服务
查看>>
Android从零开始(第四篇)网络框架MVP+Retrofit+Rxjava
查看>>
Android逆向从未如此简单
查看>>
从Android Studio 开始的ARCore之旅
查看>>
Android轮播图控件CustomBanner的使用讲解
查看>>
让你在服务器上顺风顺水——linux常用命令
查看>>
[iOS] [OC] NSNotificationCenter 进阶及自定义(附源代码)
查看>>
Python logging 库的『完整教程』
查看>>
springboot -- 2.0版本自定义ReidsCacheManager的改变
查看>>
应用层,了解一下
查看>>
Failed to execute aapt
查看>>
创建个人cocopods集合(仅供参考)
查看>>
OAuth2.0认证授权workflow探究
查看>>
《Android Security Internals》第二章权限翻译
查看>>
笔者介绍
查看>>
spring-cloud Sleuth
查看>>
大数据成神之路
查看>>