欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

巨坑总结:ModuleNotFoundError: No module named ‘tools‘引发的一系列问题

程序员文章站 2023-11-11 17:35:22
调试接口自动化Case时,在Pycharm IDE运行没有问题ipython3 ArgentinaAccess.pyTraceback (most recent call last): File "ArgentinaAccess.py", line 7, in import toolModuleNotFoundError: No module named 'tool'python3 ArgentinaAccess.pyTraceback (mos......

背景:python3版本在调试接口自动化Case时,在Pycharm IDE运行没有问题,可以正常运行py脚本,但是在终端和Docker机器上执行脚本时,一直提示 No module name 'tools'

python3 test1.py
Traceback (most recent call last):
File "test1.py", line 7, in <module>
import tools
ModuleNotFoundError: No module named 'tools'


问题定位思路:

1、python包无法引用,每个文件夹下添加__init__.py类,模块导入方式修改为:import tools.tools,调用方式都正确,但是仍然无效

2、新建一个虚拟环境,将该项目的测试环境隔离,然后在虚拟环境下执行脚本,仍然报错

python -m venv venv,内置venv模块,针对该项目创建的新的虚拟环境

python3 -m venv venv
tree -L 2
.
├── Instruction_Install
├── ReadMe
├── common
│   ├── __init__.py
│   ├── log_test.py
│   └── test.log
├── src
│   ├── test1.py
│   ├── test2.py
│   └── �\227�\225�\230�\233�
├── tools
│   └── tool.py
├── utils
│   └── AssertUtil.py
└── venv
    ├── bin
    ├── include
    ├── lib
    └── pyvenv.cfg

#激活虚拟环境,进入venv/bin下
activate		activate.fish		easy_install-3.7	pip3			python
activate.csh		easy_install		pip			pip3.7			python3

pyExercise % sh ./venv/bin/activate 


3、在终端执行脚本,报错如下,进入目录:/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tools

python3 test1.py
Traceback (most recent call last):
File "test1.py", line 8, in <module>
from tools import tools
ImportError: cannot import name 'tools' from 'tools' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tools/__init__.py)

进入目录后,看到系统内置文件,确定是因为命名重复导致,(此处有崩溃的感觉~~),项目模块名称tools修改为 ToolsUtils,重新导入包名和方法,问题解决

didi@ShirleydeMacBook-Pro tools % cd /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tools
didi@ShirleydeMacBook-Pro tools % tree -L 1
.
├── __init__.py
├── __pycache__
├── const.py
├── content.py
├── control.py
├── debug.py
├── encoding.py
├── error.py
├── etree.py
├── export
├── feed.py
├── files.py
├── google.py
├── html.py
├── http.py
├── internal.py
├── lock.py
├── logs.py
├── metric.py
├── parser.py
├── ping.py
├── progress.py
├── pwork.py
├── py3k_support.py
├── rex.py
├── russian.py
├── selenium_tools.py
├── structured.py
├── system.py
├── text.py
├── user_agent.py
├── w3lib_encoding.py
├── watch.py
├── work.py
└── yandex.py

2 directories, 33 files


4、另外一种解决方法:将项目的跟路径加入到python的环境变量路径

可以看到,python console中选择 Add content roots to PYTHONPATH,默认会加入项目到跟路径中

import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])

巨坑总结:ModuleNotFoundError: No module named ‘tools‘引发的一系列问题


代码引入包的模块加入代码:

import sys,os
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)

服务器和终端执行脚本,可以正确识别包名。


结果分析:

整体对Python语言基本环境理解不透彻,导致没有定位到问题根因,还需要把这一个整理清楚,需要有深度学习能力。

本文地址:https://blog.csdn.net/xinyuqing/article/details/107332329