python3——跨文件调用模块

1、文件目录

 在test02.py文件中调用test01.py文件中的ceshi()函数

2、参考代码:

test01:

def ceshi():
    from selenium import webdriver
    option = webdriver.ChromeOptions()
    option.add_argument('headless')  # 静默模式
    # 打开chrome浏览器
    driver = webdriver.Chrome(chrome_options=option)
    driver.get("http://www.baidu.com")
    print(driver.title)

test02参考代码:

import sys
from os.path import dirname, abspath

project_path = dirname(dirname(abspath(__file__))   # __file__用于获取文件所在的路径,调用os.path下面的abspath(__file__),可以得到文件的绝对路径
sys.path.append(project_path + '\0506') from test01 import ceshi ceshi()
原文地址:https://www.cnblogs.com/yuer02/p/12836214.html