Python 导入错误:错误“不是包”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38454852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: with error 'is not a package'
提问by agnel
In python 3 getting into ImportError issues. My project structure is like:
在 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
In scripts/cts_sap_polaris.py
I am trying an import
在scripts/cts_sap_polaris.py
我尝试导入
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.
回答by jersey bean
Rename cts_sap_polaris.py to something else.
将 cts_sap_polaris.py 重命名为其他名称。
This name conflicts with the package name (which has the same name).
此名称与包名称(具有相同名称)冲突。
(credit goes to @jedwards on his comment)
(感谢@jedwards 的评论)
回答by nagaraj bhat
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
据我了解,python 只搜索当前目录和 sys.path。所以你可以在运行时添加到python路径中。已经回答了一个类似的问题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.
让我知道它是否有效。