簡體   English   中英

如何訪問由 `pip --user` 安裝的包?

[英]How do I access packages installed by `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'

安裝的版本go去哪了?

根據Python 文檔,這是使用“用戶方案”進行安裝:

文件將安裝到site.USER_BASE 的子目錄中(以下寫為userbase )。

您可以像這樣看到您的 USER_BASE 值:

$ python -c "import site; print(site.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
<module 'numpy' from '/Users/csaftoiu/Library/Python/2.7/lib/python/site-packages/numpy/__init__.pyc'>
>>> numpy.version.full_version
'1.11.1'
>>>

@克勞迪烏的解決方案效果很好,更干凈一點是:

python3 -m site --user-base

例如在你的~/.profile

PATH="$(python3 -m site --user-base)/bin:${PATH}"

使用 --user 標志,如

pip list --user

來源: https://pip.pypa.io/en/stable/cli/pip_list/

我使用sudo pip install --user選項安裝了軟件包,我必須執行sudo python才能使其正常工作。

vishnu$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

使用 sudo:

vishnu$ sudo python
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import rasa_core
>>> 

沒有 sudo:

vishnu$ python
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import rasa_core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named rasa_core

PS:我在 Mac OS High Sierra 上運行這些,我的問題是rasa_core和依賴包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM