P4实验问题 解决python模块导入

参考:Python导入自定义包或模块

在执行./run_demo.sh的过程中,遇到了python的模块问题:

root@ubuntu:/home/wasdns/tutorials/SIGCOMM_2015/source_routing# ./run_demo.sh 
./run_demo.sh: line 31: /home/wasdns/tutorials/SIGCOMM_2015/source_routing/home/wasdns/p4c-bmv2/p4c_bm/__main__.py: No such file or directory
Traceback (most recent call last):
  File "topo.py", line 23, in <module>
    from p4_mininet import P4Switch, P4Host
ImportError: No module named p4_mininet

p4_mininet.py所在的文件夹:/home/wasdns/bmv2/mininet

root@ubuntu:/home/wasdns# cd bmv2/
root@ubuntu:/home/wasdns/bmv2# ls
aclocal.m4      config.status  include          Makefile.in  test-driver
autogen.sh      config.sub     install_deps.sh  mininet      tests
autom4te.cache  configure      install-sh       missing      third_party
compile         configure.ac   libtool          pdfixed      thrift_src
config.guess    CPPLINT.cfg    LICENSE          py-compile   tools
config.h        depcomp        ltmain.sh        README.md    travis
config.h.in     docs           m4               src          VERSION
config.h.in~    Doxyfile       Makefile         stamp-h1
config.log      Doxymain.md    Makefile.am      targets
root@ubuntu:/home/wasdns/bmv2# cd mininet/
root@ubuntu:/home/wasdns/bmv2/mininet# ls
1sw_demo.py         simple_router.p4          stress_test_ipv4.py.in
p4_mininet.py       stress_test_commands.txt
simple_router.json  stress_test_ipv4.py

于是要把这个路径加入python的系统路径中,过程如下:

1.先找到python的系统路径:

root@ubuntu:/home/wasdns# python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/mininet-2.3.0d1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/setuptools-32.1.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
>>> exit()

找到/usr/local/lib/python2.7/dist-packages这个路径。

cd /usr/local/lib/python2.7/dist-packages

2.增添.pth文件

root@ubuntu:/home/wasdns# cd /usr/local/lib/python2.7/dist-packages
root@ubuntu:/usr/local/lib/python2.7/dist-packages# touch p4_mininet.pth
root@ubuntu:/usr/local/lib/python2.7/dist-packages# vim p4_mininet.pth

/home/wasdns/bmv2/mininet

原因:

Python 在遍历已有的库文件目录(sys.path中指定)过程中,如果见到一个 .pth 文件,就会将该文件中所记录的路径加入到 sys.path 设置中,这样 .pth 文件说指明的库也就可以被 Python 运行环境找到。

3.查看路径,验证是否加入:

root@ubuntu:/usr/local/lib/python2.7/dist-packages# python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/mininet-2.3.0d1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/setuptools-32.1.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg', '/home/wasdns/bmv2/mininet', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

发现:'/home/wasdns/bmv2/mininet',解决问题。

2016/12/18

posted @ 2016-12-18 17:30  Wasdns  阅读(1880)  评论(0编辑  收藏  举报