我试图导入另一个文件夹中的包,它在python3.4中运行得非常好。例如:这些文件存在于libraries文件夹中user> python
Python 3.4.1 (default, Nov 12 2014, 13:34:29)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from libraries.controller_utils import *
>>>
但是,当我打开一个新的shell并使用python2.7时:
^{pr2}$
我尝试将条目添加到sys.path中,但没有帮助。我读过一个类似的问题here,但是这个解决方案对我也没有帮助,因为我尝试了相对和绝对导入。请指教。在
编辑:目录结构是~/tests/libraries/controller_utils.py。我在tests目录中执行这些命令。在
编辑:我添加了系统路径输入如下,但仍无法识别。请注意,错误发生在2.7版本,但在3.4版本中运行得非常好user> cd ~/tests/
user> ls
__pycache__ backups inputs libraries openflow.py test_flow.py
user> ls libraries/
__pycache__ controller_utils.py general_utils.py general_utils.pyc tc_name_list.py test_case_utils.py
user> python
Python 2.7.4 (default, Jun 1 2015, 10:35:58)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from libraries.controller_utils import *
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named libraries.controller_utils
>>> import sys
>>> sys.path.append('libraries/')
>>> from libraries.controller_utils import *
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named libraries.controller_utils