首页 > 解决方案 > 如何在cmd中使用包执行python代码?

问题描述

project
    __init__.py
    module_1
    module_2

    test_pkg
        __init__.py
        test_module_1  ----> import module_1 
        test_module_2  ----> import module_2 

我正在使用上述结构,它在 Eclipse IDE 中运行良好。但我无法从命令提示符运行。

普通的python模块可以如下执行,

python test_code.py

但是当我运行 test_module_1 或 test_module_2 时显示以下错误,

Traceback (most recent call last):
  File "test_module_1.py", line 8, in <module>
    from project.module_1 import module_1
ModuleNotFoundError: No module named 'module_1'

如何做到这一点?

标签: pythonimportpackage

解决方案


推荐阅读