且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python“导入错误:未命名模块"问题

更新时间:2022-11-15 16:22:46

这有点猜测,但我认为你需要更改您的 PYTHONPATH 环境变量以包含 src 和测试目录.

This is a bit of a guess, but I think you need to change your PYTHONPATH environment variable to include the src and test directories.

src 目录中运行的程序可能一直在运行,因为 Python 会自动将其当前运行的脚本的目录插入到 sys.path 中.因此,只要您还执行驻留在 src 中的脚本,就可以在 src 中导入模块.

Running programs in the src directory may have been working, because Python automatically inserts the directory of the script it is currently running into sys.path. So importing modules in src would have worked as long as you are also executing a script that resides in src.

但是现在您正在从 test 运行脚本,test 目录会自动添加到 sys.path,而 src 不是.

But now that you are running a script from test, the test directory is automatically added to sys.path, while src is not.

PYTHONPATH 中列出的所有目录都被添加到 sys.path,Python 搜索 sys.path 以查找模块.

All directories listed in PYTHONPATH get added to sys.path, and Python searches sys.path to find modules.

另外,如果你说

from src import Matrix

然后 Matrix 将引用包,您需要说 Matrix.Matrix 才能访问该类.

then Matrix would refer to the package, and you'd need to say Matrix.Matrix to access the class.