python修改sys.path不起作用

编程入门 行业动态 更新时间:2024-10-27 12:39:27
本文介绍了python修改sys.path不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 /opt/lib/python2.7/site-packages 下有一个新的 numpy 版本,在 /usr/lib/python2.7/下有一个标准(系统)版本dist 包.我想暂时使用新的 numpy 版本,所以我在脚本的开头添加了以下内容:

I have a new numpy version under /opt/lib/python2.7/site-packages, and a standard (system) version under /usr/lib/python2.7/dist-packages. I want to temporarily use the new numpy version so I add the following at the beginning of my script:

In [1]: import sys In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages') In [3]: sys.path Out[3]: ['', '/opt/numpy/lib/python2.7/site-packages', '/usr/local/bin', '/usr/lib/python2.7/dist-packages', '/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/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/dist-packages/IPython/extensions']

奇怪的是它仍然加载旧的 numpy 版本:

Strangely it still loads the old numpy version:

In [4]: import numpy as np In [5]: np.__file__ Out[5]: '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'

有人对此有任何想法吗?

Does anyone has any idea about this?

注1:这个问题是关于python中的sys.path.请不要推荐我使用 virtualenv 或更改 PYTHONPATH.

Note1: This question is about sys.path in python. Please don't recommend me to use virtualenv or change PYTHONPATH.

注2:在我新建的numpy目录下找到以下文件

Note2: the following files are found under my new numpy directory

$ ls /opt/numpy/lib/python2.7/site-packages easy-install.pth numpy-1.11.0-py2.7-linux-x86_64.egg __pycache__ site.py site.pyc

推荐答案

显然我必须将 numpy .egg 文件添加到 sys.path 中才能使其工作.

Apparently I have to add the numpy .egg file to sys.path in order to make it work.

In [1]: import sys In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg') In [3]: sys.path Out[3]: ['', '/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg', '/usr/local/bin', '/usr/lib/python2.7/dist-packages', '/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/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/dist-packages/IPython/extensions'] In [4]: import numpy as np In [5]: np.__file__ Out[5]: '/opt/numpy/lib/python2.7/site-packages/numpy-1.11.0-py2.7-linux-x86_64.egg/numpy/__init__.pyc'

如果我只添加站点包,它不起作用:

It doesn't work if I add the site-packages only:

In [1]: import sys In [2]: sys.path.insert(1,'/opt/numpy/lib/python2.7/site-packages') In [3]: sys.path Out[3]: ['', '/opt/numpy/lib/python2.7/site-packages', '/usr/local/bin', '/usr/lib/python2.7/dist-packages', '/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/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/dist-packages/IPython/extensions'] In [4]: import numpy as np In [5]: np.__file__ Out[5]: '/usr/lib/python2.7/dist-packages/numpy/__init__.pyc'

更多推荐

python修改sys.path不起作用

本文发布于:2023-11-23 21:19:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   python   sys   path

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!