如何访问由 `pip --user` 安装的软件包?
如何访问由 `pip --user` 安装的软件包?
我意识到我使用的numpy版本已经过时了:
$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.version.full_version
'1.8.0rc1'
我尝试进行更新,但由于某种原因我无法在整个机器上进行安装:
$ sudo pip install -U numpy
Password:
Downloading/unpacking numpy from https://pypi.python.org/packages/dd/9f/cd0ec9c50e4ed8650901ad4afde164e5252b6182a9e0c7bff5f8b4441960/numpy-1.11.1.zip#md5=5caa3428b24aaa07e72c79d115140e46
Downloading numpy-1.11.1.zip (4.7MB): 4.7MB downloaded
...
Found existing installation: numpy 1.8.0rc1
Uninstalling numpy:
Cleaning up...
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/basecommand.py", line 134, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/commands/install.py", line 241, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/req.py", line 1294, in install
requirement.uninstall(auto_confirm=True)
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/req.py", line 525, in uninstall
paths_to_remove.remove(auto_confirm)
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/req.py", line 1639, in remove
renames(path, new_path)
File "/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg/pip/util.py", line 294, in renames
shutil.move(old, new)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-fajcj_-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'
Storing complete log in /Users/csaftoiu/Library/Logs/pip.log
好吧,那我就安装到--user
目录下:
$ pip install -U --user numpy
...
Successfully installed numpy
但是版本没有更新!
$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.version.full_version
'1.8.0rc1'
安装的版本去哪了?
问题的出现原因是使用了"用户方案"进行安装,这导致安装的文件被放置在了用户的个人文件夹下。通过执行`python -c "import site; print(site.USER_BASE)"`命令可以查看`USER_BASE`的值,可以发现在我的机器上,`USER_BASE`的值是`/Users/csaftoiu/Library/Python/2.7`。然而,我发现这个路径在`sys.path`中的位置是在全局安装目录之后。为了解决这个问题,我在`~/.bash_profile`文件中添加了以下内容:
# add user base to python path export PYTHONPATH=$(python -c "import site, os; print(os.path.join(site.USER_BASE, 'lib', 'python', 'site-packages'))"):$PYTHONPATH
这样,最新的版本就可以被正确加载了。通过执行`python`命令,可以确认最新版本已经被加载:
Python 2.7.10 (default, Oct 23 2015, 18:05:06) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy>>> numpy.version.full_version '1.11.1' >>>
也可以使用`export PYTHONPATH=$(python -c "import site; print(site.USER_SITE)"):$PYTHONPATH`来设置`PYTHONPATH`,这样可以直接获得包的路径。