您的设置中有几个混淆的要求,但我会尽量满足您的要求。

首先,如果要从man1test.py访问man1.py并从man1.py访问manModules.py,则需要将文件正确设置为packages and modules。Packages are a way of structuring Python’s module namespace by using

“dotted module names”. For example, the module name A.B designates a

submodule named B in a package named A.

...

When importing the package, Python searches through the directories on

sys.path looking for the package subdirectory.

The __init__.py files are required to make Python treat the

directories as containing packages; this is done to prevent

directories with a common name, such as string, from unintentionally

hiding valid modules that occur later on the module search path.

你需要设置成这样:man

|- __init__.py

|- Mans

|- __init__.py

|- man1.py

|- MansTest

|- __init.__.py

|- SoftLib

|- Soft

|- __init__.py

|- SoftWork

|- __init__.py

|- manModules.py

|- Unittests

|- __init__.py

|- man1test.py

对于由man1test.py中的from ...Mans import man1引起的“ModuleNotFoundError: No module named 'Soft'”错误,文档化的解决方案是将man1.py添加到sys.path,因为Mans不在mantest包中。请参阅Python文档中的The Module Search Path。但如果不想直接修改sys.path,也可以修改PYTHONPATH:sys.path is initialized from these locations:The directory containing the input script (or the current directory when no file is specified).

PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

The installation-dependent default.

第三个,对于from ...MansTest.SoftLib import Soft,您说“是为了简化man1.py中的上述导入语句,这就是导入的工作方式。如果要导入man1.py中的Soft.SoftLib,则必须设置man1.py以找到Soft.SoftLib并直接导入。

这么说吧,我是怎么做到的。

男士1.py:from Soft.SoftWork.manModules import *

# no change to import statement but need to add Soft to PYTHONPATH

def foo():

print("called foo in man1.py")

print("foo call module1 from manModules: " + module1())

Man1测试# no need for "from ...MansTest.SoftLib import Soft" to facilitate importing..

from ...Mans import man1

man1.foo()

manModules.py版def module1():

return "module1 in manModules"

终端输出:$ python3 -m man.MansTest.Unittests.man1test

Traceback (most recent call last):

...

from ...Mans import man1

File "/temp/man/Mans/man1.py", line 2, in

from Soft.SoftWork.manModules import *

ModuleNotFoundError: No module named 'Soft'

$ PYTHONPATH=$PYTHONPATH:/temp/man/MansTest/SoftLib

$ export PYTHONPATH

$ echo $PYTHONPATH

:/temp/man/MansTest/SoftLib

$ python3 -m man.MansTest.Unittests.man1test

called foo in man1.py

foo called module1 from manModules: module1 in manModules

作为一个建议,也许可以重新考虑那些SoftLib文件的用途。是不是在man1.py和man1test.py之间有某种“桥梁”?你现在的文件设置方式,我认为它不会像你期望的那样工作。另外,被测代码(man1.py)从测试文件夹(mantest)下导入内容也有点混乱。

py导入包异常跳出_Python:尝试从导入的包导入模块时出现“ModuleNotFoundError”相关推荐

  1. py导入包异常跳出_python~异常处理及包

    程序里的异常:"不正常",当python检测一个错误时,解释器无法执行下去,反而出现一个错误的提示. 异常处理:try:可能出现问题的代码 Except:如果出现问题,则执行该代码 ...

  2. py导入包异常跳出_Python运行Unittest作为包导入错误

    I.前言:应用程序的目录结构和模块在文章末尾列出. 二.问题陈述:如果未设置PYTHONPATH,则应用程序运行,但是单元测试失败,并出现ImportError:没有名为models.transact ...

  3. py导入包异常跳出_14-0-包的介绍及使用

    1.包介绍 1.1.什么是包? 官网解释 Packages are a way of structuring Python's module namespace by using "dott ...

  4. py导入包异常跳出_马克的Python学习笔记#模块和包

    把模块按层次结构组织成包 模块和包是任何大型项目的核心,就连Python安装程序它本身也是一个包.创建一个软件包结构是非常简单的,我们只要把代码按照我们所希望的方式在文件系统上进行组织并且确保每个目录 ...

  5. python常用包及其用法_Python 使用的部分常用包以及数据分析pandas及工具jupyter使用...

    常用的几个包如下 #fabric for remote server control控制远程机器以及执行命令 #argparse for --helper 帮助函数 #requests for htt ...

  6. python建立数据库连接时出错_python – 尝试连接到localhost上的数据库时出现pyodbc连接错误...

    我的机器上有一个名为"Test"的本地数据库,其中包含一个名为"Tags"的表.我能够通过SQL Server管理工作室2008从该表访问此数据库和查询. 但是 ...

  7. Spring——AOP配置时的jar包异常

    Spring--AOP配置时的jar包异常 参考文章: (1)Spring--AOP配置时的jar包异常 (2)https://www.cnblogs.com/dyllove98/p/3180193. ...

  8. 高通5G平台(SDX55\SDX62\SDX65):ping包异常问题排查指南

    高通5G平台:ping包异常问题排查指南 1. 背景 2. Ping包数据流走向及网络架构 2.1 终端与网络架构图 2.2 终端与基站之间协议栈数据流走向图 3. Ping包问题常见分析思路 3.1 ...

  9. python全栈工程师能接到私活么_Python全栈工程师(包、模块 的导入)

    ParisGabriel 每天坚持手写  一天一篇  决定坚持几年 为了梦想 为了信仰 Python人工智能从入门到精通 $ pip3 install tensorflow : 第三方模块tensor ...

最新文章

  1. Mac上搭建Nginx + rtmp
  2. 数据同步关于去除乱码插入mysql数据库
  3. 2022版全球及中国电梯行业投资建议与盈利价值分析报告
  4. 获取某年某月的第一天和最后一天的Sql Server函数
  5. python可以在linux运行_服务器(Linux)上运行python总结
  6. ghostwin10系统后不小心重新分区了,其它分区的资料如何找回
  7. 明晚直播预告丨Oracle 19c X86下移经验分享
  8. 计算机老师的专业发展怎么写,高职计算机教师专业发展研究
  9. Navicat Premium 15注册出现“rsa public key not find”
  10. EXCEL选择下拉框实现复选
  11. 计算机朝微型化网络化发展例子,收集计算机朝微型化,网络化,智能化和多功能化发展的例子...
  12. html submit没有提交数据
  13. EditText修改软键盘输入法的Enter键的按钮文字
  14. java中的递归算法_java递归算法实例分析
  15. linux转录组分析,完整转录组RNAseq分析流程(tophat2+cufflink+cuffdiff)
  16. 除霾神器:中科爱讯推出BHU智能新风机,让你呼吸森林空气
  17. 高端进销存管理系统源码+微信小程序,
  18. 普通人怎样投资区块链
  19. 中山纪念中学2015年暑假集训总结
  20. 位图排序算法优化篇-永无止境

热门文章

  1. LNMP安装目录及配置文件
  2. 读Zepto源码之代码结构
  3. Python学习【第二篇】 : Python入门
  4. 乱码解决方案SecureCRT中文乱码解决方案
  5. linux常用命令(ubuntu)
  6. VS2010 C# 4.0新特性一览
  7. 52 - 算法 - 数据结构 vector
  8. 十大经典排序算法2(Python版本)
  9. (篇五)C语言水仙花数的花样求法
  10. 在安卓手机上编写和运行Python 3.x程序