总结:解决ModuleNotFoundError: No module named ‘tools‘问题

本文介绍了在Python中遇到`ModuleNotFoundError: No module named 'tools'`的问题,包括问题背景、定位思路及解决方案。通过检查`__init__.py`、使用虚拟环境、避免命名冲突和修改环境变量等方法,最终成功解决问题。
摘要由CSDN通过智能技术生成

背景: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])

 

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

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

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

 

结果分析:

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

 

 

 

 

 

 

 

 

 

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值