参考: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

最新文章

  1. HDOJ 4770 Lights Against Dudely
  2. AMD and CMD are dead之KMDjs在JS工程化的努力
  3. 学习MySQL之单表操作(二)
  4. POJ 1002
  5. this.Invoke
  6. windows 8.1 MessageDialog
  7. Hashtable与HashMap区别(2)
  8. 关于Log4j的初始化
  9. isupper()函数
  10. FastCGI
  11. 开启CSP网页安全政策防止XSS攻击
  12. vue 组件通信
  13. Mysql完整约束性
  14. java 注解 知识整理
  15. Custom Default Node Colors and Shapes in Houdini 16.5
  16. python全栈开发 * 31知识点汇总 * 180716
  17. Repository mirror 解析
  18. SSIS 连接数据
  19. [翻译]NUnit---TearDown and SetUpFixture and Test Attributes(二十)
  20. ORACLE判断两个日期间隔几个工作日

热门文章

  1. [工作中的设计模式]桥接模式bridge
  2. Python 学习笔记01
  3. Hadoop 2.0安装以及不停集群加datanode
  4. 设置某个ip对mysql服务器有权限,以及mysql定时备份
  5. jQuery入门第二天
  6. 3分钟4个步骤超级简单入门配置lamp
  7. 【HDU】3853 LOOPS
  8. BZOJ4503: 两个串
  9. SDL 五子棋游戏
  10. 人工智能 --test