我就廢話不多說了,大家往下看吧~
P
|-A
| a.py
|-B
| b.py
文件結(jié)構(gòu)如上,在windows下
a想運(yùn)行b中的方法,可以直接寫成
但是在linux中會出現(xiàn)
ModuleNotFoundError: No module named 'B'
可以通過引入sys添加路徑解決
import sys
sys.path.append("/P/B")
from B impot b
在程序中為了也有良好的移植性,可以在代碼中顯式添加路徑,
比如用os的方法:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
或者pathlib 方法:
BASE_DIR = Path(__file__).resolve().parent.parent
最后可以添加到 sys.path中
sys.path.append(str(BASE_DIR))
補(bǔ)充:python import導(dǎo)入三種方式(導(dǎo)入不同文件夾的文件,window和linux的區(qū)別)
import導(dǎo)入方式
1、導(dǎo)入python自有的庫
2、導(dǎo)入同級目錄下的文件,比如在logindev.py導(dǎo)入read_config.py文件
# coding = utf-8
from read_config import ReadConfig
import requests
import json
# 拼接請求參數(shù),獲取cookie,作為后續(xù)登錄使用
3、導(dǎo)入不同級別目錄下的文件:
a、windows環(huán)境
# coding = utf-8
import datetime
import allure
import pytest
import json
import sys
"""windows執(zhí)行的時(shí)候,直接在系統(tǒng)路徑上新增文件夾路徑"""
sys.path.append("../common")
"""linux執(zhí)行的時(shí)候,直接按照相對路徑引用即可"""
from common import http_request
這段代碼在linux下報(bào)如下錯誤:
==================================== ERRORS ====================================
_______________ ERROR collecting test_case/test_member_ticket.py _______________
ImportError while importing test module '/var/jenkins_home/workspace/pytest_allure/test_case/test_member_ticket.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_member_ticket.py:8: in module>
from common import http_request
E ImportError: No module named 'common'
b、linux環(huán)境
# coding = utf-8
import datetime
import allure
import pytest
import json
import sys
"""windows執(zhí)行的時(shí)候,直接在系統(tǒng)路徑上新增文件夾路徑"""
#sys.path.append("../common")
"""linux執(zhí)行的時(shí)候,直接按照相對路徑引用即可"""
from ..common import http_request
這段代碼在windows報(bào)如下錯誤:
========================================================================== ERRORS ==========================================================================
__________________________________________________________ ERROR collecting test_member_ticket.py __________________________________________________________
test_member_ticket.py:8: in module>
from ..common import http_request
E ValueError: attempted relative import beyond top-level package
================================================================= short test summary info ==================================================================
ERROR test_member_ticket.py - ValueError: attempted relative import beyond top-level package
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================== 1 error in 0.46s =====================================================================
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python腳本激活Linux密碼的方法(crypt模塊)
- python滲透測試linux密碼激活的示例
- python獲取linux和windows系統(tǒng)指定接口的IP地址的步驟及代碼
- linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析
- 在Linux下使用命令行安裝Python