ImportError: with error 'not a package'

In python 3, they get into ImportError problems. My project structure is similar:

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

Q scripts/cts_sap_polaris.pyI'm trying to import

import cts_sap_polaris.lib.cli_check as cli_check

What causes this error:

ImportError: No module named 'cts_sap_polaris.lib'; 'cts_sap_polaris' is not a package.
+4
source share
1 answer

From what I understand, python is only looking for the current directory and sys.path. This way you can add to the python path at runtime. The answer to a similar question was here

I suggest you try this ..

    # 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

Let me know if it works.

0
source

Source: https://habr.com/ru/post/1648356/


All Articles