024模块的概念

栏目: Python · 发布时间: 7年前

内容简介:024模块的概念

###模块

calculate是自己写的,出现红色也可以调用

import   calculate      # 通过搜索路径找到calculate.py后,将calculate = calculate.py(的全部代码)
print(calculate.add(4,5))

from  calculate  import *
print(add(3,4),'    ',sub(3,4))

from  calculate  import  add  as  plus
print(plus(3,4))

###包

导入包中的模块

from  web  import  logger
logger.logger()

导入包中包的模块

from  web.web2  import  logger
logger.logger()

导入包中包模块的方法

from  web.web2.logger  import  logger
logger()

导入包解释了__init__文件

导入模块和包的区别,导入模块把模块解释了一遍,导入包只是解释了__init__文件

###项目中的模块导入比较复杂

简单目录结构,最后执行bin.py

024模块的概念

logger

024模块的概念

main

024模块的概念

这里使用from module import  logger的原因是因为在bin下执行的,工作目录在那里,但是还是要使用sys模块添加工作路径。

#bin

# 所以就有了下面的步骤

import  sys,os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   #拿到这个路径的上两层路径,也就是atm_demo路径
print(BASE_DIR)          #F:\python_code\atm_demo
sys.path.append(BASE_DIR)    #之所以在这里添加,是因为
from  module  import  main
main.main()

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Uberland

Uberland

Alex Rosenblat / University of California Press / 2018-11-19 / GBP 21.00

Silicon Valley technology is transforming the way we work, and Uber is leading the charge. An American startup that promised to deliver entrepreneurship for the masses through its technology, Uber ins......一起来看看 《Uberland》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具