【问题标题】:installing and importing python packages安装和导入python包
【发布时间】:2014-02-02 12:28:50
【问题描述】:

我是一个相对较新的 python 用户,我一直遇到这个(也许很简单?)问题,它确实阻碍了我取得任何进步。

这就是问题所在。每当我尝试使用 pip 或 easy_install 安装软件包时,我都会收到错误消息,指出我的用户配置文件无权访问安装目录。但是,我是计算机的管理员。使用 easy_install 安装永远不会完成(我得到下面的错误),但它最终使用 pip 完成,但我无法在安装后导入包 - 我得到通常的“没有名为 [模块名称] 的模块”错误。

Easy_install error message:
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/test-easy-install-3280.write-test'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/

Perhaps your account does not have write access to this directory?  If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.  If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.

For information on other options, you may wish to consult the
documentation at:

  http://packages.python.org/distribute/easy_install.html

Please make the appropriate changes for your system and try again.

【问题讨论】:

  • 看起来我的站点包文件夹在 python 2.7 文件夹中是空的。那是安装的软件包应该在哪里吗?我已经尝试将模块的文件夹移动到这个位置,重新启动 python 看看是否有任何效果,但没有。

标签: python-2.7 pip packages easy-install


【解决方案1】:

与 virtualenv 相比,一个更简单的可能性是通过键入以下内容将包安装到主目录中:

easy_install --user <package> 

您的包将安装在 ~/.local/lib/python2.7 中,它位于 python 包的默认路径中。

这与平台无关。从你的输出我猜你正在使用 Linux。在 Windows 上,如果您使用标准 Python 安装,easy_install 始终需要管理员权限。

【讨论】:

  • 很好的答案!简洁明了。让我省了很多麻烦:)
【解决方案2】:

您需要使用sudo,因为您尝试安装在全局包文件夹 (/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/) ,并且您的用户显然没有该文件夹的写入权限。

虽然sudo 将帮助您克服该限制,但请查看virtualenv 以创建单独、独立的环境,您可以在其中安装不同的软件包而不会重叠。此外,OS X 附带的 Python 显然是一个修改版本,因此您最好安装一个完整的独立版本。这样一来,如果 Apple 升级 Python 版本(或不升级,您需要不同的版本),您将不会受到影响。

我发现this article 非常适合让我在 OS X 上使用 Python。

【讨论】:

  • sudo easy_install 通常是个坏主意,因为 easy_install 不会尝试确保安全。
  • 感谢 Kristof,我按照文章中的指示一步步操作,包括将我的操作系统更新为小牛,但问题似乎刚刚被放大了。每当我尝试导入我知道已安装的软件包(numpy、scipy、matplotlib)时,python 都会崩溃。我可以在 ipython 中导入这些包,但不能在 python 本身中导入。知道发生了什么吗?
  • 首先,通过运行which python 检查您是否使用了正确的python。另外,看看shebang,看看它是否也指向正确的python位置。查看this问题(和答案)了解更多信息。
猜你喜欢
  • 2016-02-07
  • 2020-12-02
  • 2020-05-20
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2016-09-12
相关资源
最近更新 更多