Python 模块之间的调用

Posted Alex0425

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 模块之间的调用相关的知识,希望对你有一定的参考价值。

user_main调用handle模块中的home方法,导入命令:

from backend.login import handle

handle模块中使用到了sql_api模块,导入命令:

from backend.db import sql_api

这个目录里面,user_main调用了handle模块,handle模块调用了sql_api模块,sql_api模块里面使用了配置文件settings
#user_main调用了handle模块
from backend.login import handle

handle.home()

#handle模块调用了sql_api模块
from backend.db import sql_api
def home():
    print ("welcome to home page")
    q_data = sql_api.select(\'user\')
    print (q_data)

def movie():
    print ("welcome to movie page")

def tv():
    print ("welcome to tv page")

#sql_api模块中的db_auth方法使用了settings配置文件
from configure import settings
def db_auth(configs):
    if configs.DATABASE[\'user\'] == \'root\' and configs.DATABASE[\'password\'] == \'123\':
        print("db auth successed")
    else:
        print("db login failed")

def select(table):
    db_auth(settings)
    if table == \'user\':
        return table

打印结果

welcome to home page
db auth successed
user

 

#################################################################################################

前面我们都是从父模块调用到子模块,现在我们尝试着从子模块调用其他父模块的子模块,例如我们直接执行sql_api模块,该模块需要调用settings参数

from configure import settings
def db_auth(configs):
    if configs.DATABASE[\'user\'] == \'root\' and configs.DATABASE[\'password\'] == \'123\':
        print("db auth successed")
    else:
        print("db login failed")

def select(table):
    db_auth(settings)
    if table == \'user\':
        return table

#结果
Traceback (most recent call last):
  File "D:/Python/Day5/dj/backend/db/sql_api.py", line 2, in <module>
    from configure import settings
ImportError: No module named \'configure\'

我们可以通过命令查看到当前的执行模块的位置,然后计算出base的目录,然后再将base目录添加到os.path环境变量中去(list属性)


__author__ = \'Alex\'
import sys
import os
print (sys.path)
print (__file__)
print (os.path.dirname(__file__))
print (os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
base_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(base_dir)
from configure import settings
def db_auth(configs):
if configs.DATABASE[\'user\'] == \'root\' and configs.DATABASE[\'password\'] == \'123\':
print("db auth successed")
else:
print("db login failed")

def select(table):
db_auth(settings)
if table == \'user\':
return table

#结果:

[\'D:\\\\Python\\\\Day5\\\\dj\\\\backend\\\\db\', \'D:\\\\Python\', \'C:\\\\Windows\\\\system32\\\\python34.zip\', \'C:\\\\Python34\\\\DLLs\', \'C:\\\\Python34\\\\lib\', \'C:\\\\Python34\', \'C:\\\\Python34\\\\lib\\\\site-packages\']
D:/Python/Day5/dj/backend/db/sql_api.py    #__file__打印当前文件的相对路径(注意是相对,此处的绝对路径是pycharm自己添加的)
D:/Python/Day5/dj/backend/db               #os.path.dirname(__file__)去掉最后一个/后的文件名(不管后面是文件还是文件夹的名字)

D:/Python/Day5/dj                          #os.path.dirname()执行三次以后的结果

 

 

以上是关于Python 模块之间的调用的主要内容,如果未能解决你的问题,请参考以下文章

Python 模块之间的调用

如何将字符串数据从活动发送到片段?

Android中切换标签片段之间的延迟

常用python日期日志获取内容循环的代码片段

python不同包之间调用时出现文件模块不存在的问题

模块与包之间的调用