Python 如何访问“pip--user”安装的软件包?

Python 如何访问“pip--user”安装的软件包?,python,pip,package,Python,Pip,Package,我意识到我有一个过时的numpy版本: $python Python 2.7.10(默认值,2015年10月23日,18:05:06) [GCC 4.2.1达尔文兼容苹果LLVM 7.0.0(clang-700.0.59.5)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。 >>>将numpy作为np导入 >>>np.version.full_版本 “1.8.0rc1” 我试图更新它,但由于某些原因,无法在整个机器上安装: $sudo pip安装-U numpy 密码: 从

我意识到我有一个过时的numpy版本:

$python
Python 2.7.10(默认值,2015年10月23日,18:05:06)
[GCC 4.2.1达尔文兼容苹果LLVM 7.0.0(clang-700.0.59.5)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>将numpy作为np导入
>>>np.version.full_版本
“1.8.0rc1”
我试图更新它,但由于某些原因,无法在整个机器上安装:

$sudo pip安装-U numpy
密码:
从下载/解包numpyhttps://pypi.python.org/packages/dd/9f/cd0ec9c50e4ed8650901ad4afde164e5252b6182a9e0c7bff5f8b4441960/numpy-1.11.1.zip#md5=5caa3428b24aaa07e72c79d115140e46
下载numpy-1.11.1.zip(4.7MB):下载4.7MB
...
找到现有安装:numpy 1.8.0rc1
正在卸载numpy:
清理。。。
例外情况:
回溯(最近一次呼叫最后一次):
文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/basecommand.py”,第134行,在main中
status=self.run(选项、参数)
文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/commands/install.py”,第241行,运行中
需求设置.install(安装选项,全局选项,root=options.root路径)
文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/req.py”,第1294行,在安装中
要求.卸载(自动确认=True)
文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/req.py”,第525行,在卸载中
路径到移除。移除(自动确认)
文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/req.py”,第1639行,删除
重命名(路径,新路径)
重命名中的文件“/Library/Python/2.7/site packages/pip-1.4.1-py2.7.egg/pip/util.py”,第294行
shutil.移动(旧、新)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”,第302行,移动中
副本2(src、real_dst)
copy2中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”,第131行
copystat(src、dst)
copystat中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py”,第103行
os.chflags(dst、圣圣彼得堡旗帜)
OSError:[Errno 1]不允许操作:'/tmp/pip-fajcj_389;-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/Python/numpy-1.8.0rc1-py2.7.egg info'
存储完整的登录/Users/csaftoiu/Library/Logs/pip.log
好的,我将安装到
--user
,然后:

$pip安装-U——用户numpy
...
已成功安装numpy
但是版本没有更新

$python
Python 2.7.10(默认值,2015年10月23日,18:05:06)
[GCC 4.2.1达尔文兼容苹果LLVM 7.0.0(clang-700.0.59.5)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>将numpy作为np导入
>>>np.version.full_版本
“1.8.0rc1”
安装的版本到哪里去了?

根据,这是使用“用户方案”安装的:

文件将安装到的子目录中(以下写为userbase)

您可以如下所示查看用户的基本值:

$ python -c "import site; print(site.USER_BASE)"
/Users/csaftoiu/Library/Python/2.7
我发现在我的机器上,这是在
sys.path
上,但它位于全局安装目录之后

我通过将此添加到我的
~/.bash\u配置文件中解决了此问题:

# 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
PATH="$(python3 -m site --user-base)/bin:${PATH}"
现在确实加载了最新版本:

$ 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'
>>>
$python
Python 2.7.10(默认值,2015年10月23日,18:05:06)
[GCC 4.2.1达尔文兼容苹果LLVM 7.0.0(clang-700.0.59.5)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>进口numpy
>>>努比
>>>numpy.version.full\u版本
'1.11.1'
>>>

我安装了带有
sudopip安装--user
选项的软件包,我必须执行
sudopython
才能让它工作

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
vishnu$python
Python 2.7.10(默认值,2017年7月15日,17:16:57)
[GCC 4.2.1达尔文兼容苹果LLVM 9.0.0(clang-900.0.31)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入rasa_核心
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ImportError:没有名为rasa_core的模块

PS:我在Mac OS High Sierra上运行这些,我的问题是
rasa_core
和相关软件包

@Claudiu的解决方案效果很好,更干净一点的是:

python3 -m site --user-base
e、 g.在您的
~/.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
PATH="$(python3 -m site --user-base)/bin:${PATH}"

最好使用export PYTHONPATH=$(python-c“import site;print(site.USER\u site)”):$PYTHONPATHUSER\u site直接提供包路径如果使用--USER标志sudo,包将安装在根用户目录中,因为命令由根用户hi执行,那
.profile
?@Hugolpz我更新了我的答案来回答你的问题:)我已经+1e了。