ImportError:错误'不是包'

agn*_*nel 11 python python-3.x

在python 3中遇到ImportError问题.我的项目结构如下:

cts_sap_polaris/                                                                     
|-- etc                                                                              
|   |-- clean_cts_sap_polaris.yaml                                                   
|   |-- clean_env_variables.tcl                                                      
|   |-- cts_sap_polaris_ha_combined.yaml                                             
|   |-- cts_sap_polaris.yaml                                                         
|   `-- TCL_TESTBED_CONFIGS                                                          
|-- __init__.py                                                                      
|-- jobs
|   |-- __init__.py
|   |-- __pycache__
|   |   `-- run_cts_sap_polaris.cpython-34.pyc
|   `-- run_cts_sap_polaris.py
|-- lib
|   |-- cli_check.py
|   |-- cts_sap_polaris_utils.py
|   |-- __init__.py
|   |-- router_show_cts_cmd.py
|   |-- router_show_etherchannel_cmd.py
|   |-- router_show.py
|   |-- utils.py
|   |-- validate_show_output.py
|   `-- wait_for.py
|-- scripts
|   |-- cts_sap_polaris_ha_combined.py
|   |-- cts_sap_polaris.py
|   |-- __init__.py
|   `-- __pycache__
|       `-- cts_sap_polaris.cpython-34.pyc
`-- test
    |-- code_snippets
    |-- cts_interface.json
    |-- cts_interface_summary.json
    |-- etherchannel_port_channel.json
    |-- etherchannel_port.json
    |-- __init__.py
    |-- test_cts_sap_cli.py
    `-- test_router_show.py
Run Code Online (Sandbox Code Playgroud)

scripts/cts_sap_polaris.py我尝试导入

import cts_sap_polaris.lib.cli_check as cli_check
Run Code Online (Sandbox Code Playgroud)

这引发了这个错误:

ImportError: No module named 'cts_sap_polaris.lib'; 'cts_sap_polaris' is not a package.
Run Code Online (Sandbox Code Playgroud)

jer*_*ean 17

将cts_sap_polaris.py重命名为其他内容.

此名称与包名称(具有相同名称)冲突.

(在他的评论中归功于@jedwards)


nag*_*hat 1

据我了解,python只搜索当前目录和sys.path。所以你可以在运行时添加到python路径。类似的问题已在这里得到回答

我建议你尝试一下这个..

# scripts/cts_sap_polaris.py
# Add additional path to current sys path
import sys
sys.path.insert(0,'/path/to/cts_sap_polaris/lib')
import cli_check
Run Code Online (Sandbox Code Playgroud)

让我知道它是否有效。