48

In python 3 getting into ImportError issues. My project structure is like:

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

In scripts/cts_sap_polaris.py I am trying an import

import cts_sap_polaris.lib.cli_check as cli_check

Which is throwing this error:

ImportError: No module named 'cts_sap_polaris.lib'; 'cts_sap_polaris' is not a package.
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
agnel
  • 631
  • 1
  • 7
  • 9
  • 3
    From what directory are you trying to run this script? From inside the scripts directory? You'll either need to run it from the same directory as you were in when you got the tree, or add that same directory to your PYTHONPATH. – jedwards Jul 19 '16 at 09:50
  • 11
    Actually, you *would* need to do that, but in your case, the existence of the file named "cts_sap_polaris.py" *also* in the scripts directory will always be found first. It'll have to be renamed (or moved) as well. – jedwards Jul 19 '16 at 09:56
  • 2
    @jedwards: fantastic! i was banging my head over this as I was facing the same issue. Changing the file name helped as you suggested in your second comment. If you don't mind i'm going to post this as the answer as I believe this will resolve the original problem as well. – jersey bean Jul 10 '18 at 23:39

2 Answers2

79

Rename cts_sap_polaris.py to something else.

This name conflicts with the package name (which has the same name).

(credit goes to @jedwards on his comment)

jersey bean
  • 3,321
  • 4
  • 28
  • 43
1

From what i understand, python only searches the current directory and sys.path. So you can add to the python path at run time. A similar question has been answered here

I would suggest you to 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.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
nagaraj bhat
  • 124
  • 2
  • 5